Index: content_profile.module
===================================================================
--- content_profile.module	(revision 1787)
+++ content_profile.module	(working copy)
@@ -533,7 +533,7 @@
  * Implementation of hook_theme().
  */
 function content_profile_theme() {
-  return array(
+  $theme = array(
     'content_profile_display_view' => array(
       'template'  => 'content_profile-display-view',
       'arguments' => array('element' => NULL),
@@ -552,6 +552,17 @@
       'file' => 'content_profile.theme.inc',
     ),
   );
+
+  if (module_exists('imagecache')) {
+    foreach (imagecache_presets() as $preset) {
+      $theme['content_profile_formatter_'. $preset['presetname'] .'_userlink'] = array(
+        'arguments' => array('element' => NULL),
+        'function' => 'theme_imagecache_formatter_userlink',
+      );
+    }
+  }
+
+  return $theme;
 }
 
 /**
@@ -594,6 +605,45 @@
 }
 
 /**
+ * Add formatter for imagefields to be able to link images to node authors, e.g. to user profiles.
+ */
+if (module_exists('imagecache')) {
+  /**
+   * Implementation of hook_field_formatter_info().
+   */
+  function content_profile_field_formatter_info() {
+    $formatters = array();
+    foreach (imagecache_presets() as $preset) {
+      $formatters[$preset['presetname'] .'_userlink'] = array(
+        'label' => t('@preset image linked to user', array('@preset' => $preset['presetname'])),
+        'field types' => array('image', 'filefield'),
+      );
+    }
+    return $formatters;
+  }
+
+  function theme_imagecache_formatter_userlink($element) {
+    // Inside a view $element may contain NULL data. In that case, just return.
+    if (empty($element['#item']['fid'])) {
+      return '';
+    }
+
+    // Extract the preset name from the formatter name.
+    $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
+    $style = 'linked';
+
+    $item = $element['#item'];
+    $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : '';
+    $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL;
+
+    $imagetag = theme('imagecache', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title']);
+    $path = empty($item['uid']) ? '' : 'user/'. $item['uid'];
+    $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}";
+    return l($imagetag, $path, array('attributes' => array('class' => $class), 'html' => TRUE));
+  }
+}
+
+/**
  * Implementation of hook_simpletest().
  */
 function content_profile_simpletest() {
