diff --git a/cdn.fallback.inc b/cdn.fallback.inc
index 55aec64..46f3de5 100644
--- a/cdn.fallback.inc
+++ b/cdn.fallback.inc
@@ -26,11 +26,11 @@ function cdn_html_alter_image_urls(&$html) {
   $pattern .= "([\"|'][^>]*)>"; // End of opening <a> tag.
   $pattern .= "((<img\s+|<img\s+[^>]*\s+)src\s*=\s*[\"|'])([^\"|^']*)([\"|'])"; // Wrapped <img> tag.
   $pattern .= ")#i";
-  _cdn_html_alter_file_url($html, $pattern, 0, 4, 5, 1, 7, 11);
+  _cdn_html_alter_file_url($html, $pattern, 0, 4, 6, 1, 7, 11);
 
   // Image file URLs in <img> tags.
   $pattern = "#((<img\s+|<img\s+[^>]*\s+)src\s*=\s*[\"|'])($url_prefix_regex)([^\"|^'|^\?]*)()(\?[^\"|^']*)?([\"|'])#i";
-  _cdn_html_alter_file_url($html, $pattern, 0, 4, 5, 1, 7);
+  _cdn_html_alter_file_url($html, $pattern, 0, 4, 6, 1, 7);
 }
 
 /**
diff --git a/cdn.test b/cdn.test
index 02eb3d1..2f39061 100644
--- a/cdn.test
+++ b/cdn.test
@@ -25,7 +25,10 @@ class CDNTestCase extends DrupalUnitTestCase {
     $module_list = module_list();
     $module_list['cdn']['filename'] = $cdn_module_file;
     module_list(TRUE, FALSE, FALSE, $module_list);
+    $implementations = &drupal_static('module_implements');
+    $implementations = array();
 
+    $this->loadFile('cdn.constants.inc');
     $this->loadFile('cdn.module');
 
     // Override $conf to be able to use variable_set() and variable_get() in
@@ -445,3 +448,73 @@ class CDNOriginPullFarFutureTestCase extends CDNTestCase {
     $this->assertIdentical($uri, $expected, 'cdn_file_url_alter() works correctly.');
   }
 }
+
+class CDNImageTestCase extends CDNTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Image HTML altering',
+      'description' => 'Verify that image URLs inside HTML are rewritten correctly.',
+      'group' => 'CDN',
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+    $this->loadFile('cdn.fallback.inc');
+  }
+
+  function testImage() {
+    $this->variableSet(CDN_BASIC_MAPPING_VARIABLE, 'http://cdn-a.com');
+    $this->variableSet(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
+
+
+    // Simplest case possible for "<img />".
+    $image_path = base_path() . 'foo/bar/image.png';
+    $template = function($url) {
+      return '<img src="' . $url . '" />';
+    };
+    $html = $template($image_path);
+    cdn_html_alter_image_urls($html);
+    $expected = 'http://cdn-a.com' . $image_path;
+    $this->assertIdentical($template($expected), $html, 'Image HTML correctly altered.');
+
+    // Query strings should be stripped
+    $html = $template($image_path . '?foobar');
+    cdn_html_alter_image_urls($html);
+    $this->assertIdentical($template($expected), $html, 'Image HTML correctly altered (query string stripped).');
+
+    // Edge case: a script generating an image is not (yet) supported.
+    $generated_image_path = base_path() . 'showimage?formula=12345.png';
+    $html = $template($generated_image_path);
+    cdn_html_alter_image_urls($html);
+    $expected = 'http://cdn-a.com' . base_path() . 'showimage';
+    // NOTE: this is what is being requested at http://drupal.org/node/1864536.
+    //$expected = 'http://cdn-a.com' . base_path() . 'showimage?formula=12345.png';
+    $this->assertIdentical($template($expected), $html, 'Image HTML correctly altered (query string stripped).');
+
+    // Simplest case possible for "<a><img /></a>".
+    $image_base_path = base_path() . 'foo/bar/image';
+    $template = function($a_url, $img_url) {
+      return '<a href="' . $a_url . '"><img src="' . $img_url . '" /></a>';
+    };
+    $html = $template($image_base_path . '.png', $image_base_path . '-thumbnail.png');
+    cdn_html_alter_image_urls($html);
+    $expected_a_url = 'http://cdn-a.com' . $image_base_path . '.png';
+    $expected_img_url = 'http://cdn-a.com' . $image_base_path . '-thumbnail.png';
+    $this->assertIdentical($template($expected_a_url, $expected_img_url), $html, 'Linked image HTML correctly altered.');
+
+    // Ensure that images linking to content (i.e. not a bigger version of the
+    // image) don't get their anchors modified
+    $link_url = base_path() . 'node';
+    $html = $template($link_url, $image_base_path . '-thumbnail.png');
+    cdn_html_alter_image_urls($html);
+    $expected_img_url = 'http://cdn-a.com' . $image_base_path . '-thumbnail.png';
+    $this->assertIdentical($template($link_url, $expected_img_url), $html, 'Linked image HTML correctly altered (anchor unmodified).');
+
+    // Same, but now for an anchor with a query string.
+    $link_url = base_path() . 'node?foobar';
+    $html = $template($link_url, $image_base_path . '-thumbnail.png');
+    cdn_html_alter_image_urls($html);
+    $this->assertIdentical($template($link_url, $expected_img_url), $html, 'Linked image HTML correctly altered (anchor unmodified, even with query strings).');
+  }
+}
