diff --git a/hover_preview.module b/hover_preview.module
index 7bf5d90..c9dc102 100644
--- a/hover_preview.module
+++ b/hover_preview.module
@@ -82,6 +82,7 @@ function hover_preview_field_formatter_view($entity_type, $entity, $field, $inst
   // Could even pass the hover image in as a data attribute:
   //   http://ejohn.org/blog/html-5-data-attributes/
   foreach ($element as $delta => $item) {
+    $element[$delta]['#theme'] = 'hover_preview_image_formatter';
     $element[$delta]['#item']['attributes']['class'] = 'hover-preview-item';
     $element[$delta]['#item']['attributes']['data-hover-preview'] = $settings[$id][$delta];
     $element[$delta]['#attached']['js'][] = array(
@@ -110,3 +111,50 @@ function hover_preview_field_formatter_view($entity_type, $entity, $field, $inst
   return $element;
 }
 
+/**
+ * Implements hook_theme().
+ */
+function hover_preview_theme() {
+  return array(
+    'hover_preview_image_formatter' => array(
+      'variables' => array('item' => NULL, 'path' => NULL, 'image_style' => NULL),
+    ),
+  );
+}
+
+/**
+ * Copy of theme_image_formatter() with attributes support.
+ */
+function theme_hover_preview_image_formatter($variables) {
+  $item = $variables['item'];
+  $image = array(
+    'path' => $item['uri'],
+    'alt' => $item['alt'],
+  );
+  // Do not output an empty 'title' attribute.
+  if (drupal_strlen($item['title']) > 0) {
+    $image['title'] = $item['title'];
+  }
+  // This is the change from theme_image_formatter().
+  if (isset($item['attributes'])) {
+    $image['attributes'] = $item['attributes'];
+  }
+
+  if ($variables['image_style']) {
+    $image['style_name'] = $variables['image_style'];
+    $output = theme('image_style', $image);
+  }
+  else {
+    $output = theme('image', $image);
+  }
+
+  if ($variables['path']) {
+    $path = $variables['path']['path'];
+    $options = $variables['path']['options'];
+    // When displaying an image inside a link, the html option must be TRUE.
+    $options['html'] = TRUE;
+    $output = l($output, $path, $options);
+  }
+
+  return $output;
+}
