--- imagecache/imagecache.module	2009-08-19 22:59:07.000000000 +0200
+++ imagecache_coagul/imagecache.module	2011-02-27 10:12:52.000000000 +0100
@@ -66,9 +66,17 @@
  */
 function imagecache_menu() {
   $items = array();
-
+  
+  $download_mode = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
+  if ($download_mode == FILE_DOWNLOADS_PUBLIC_REPOSITORY) {
+    $imagecache_menu_path = 'imagecache';
+  }
+  else {
+    $imagecache_menu_path = sprintf('%s/imagecache', trim(file_directory_path(), '/'));
+  }
+    
   // standard imagecache callback.
-  $items[file_directory_path() .'/imagecache'] = array(
+  $items[$imagecache_menu_path] = array(
     'page callback' => 'imagecache_cache',
     'access callback' => TRUE,
     'type' => MENU_CALLBACK
@@ -321,6 +329,13 @@
       return url($GLOBALS['base_url'] . '/' . file_directory_path() .'/imagecache/'. $presetname .'/'. $path, $args);
     case FILE_DOWNLOADS_PRIVATE:
       return url('system/files/imagecache/'. $presetname .'/'. $path, $args);
+    case FILE_DOWNLOADS_PUBLIC_REPOSITORY:
+      return sprintf(
+        '%s/imagecache/%s/%s',
+        rtrim(variable_get('files_url_prefix', $GLOBALS['base_url']), '/'),
+        $presetname,
+        $path
+      );
   }
 }
 
@@ -829,13 +844,35 @@
   if (is_null($attributes)) {
     $attributes = array('class' => 'imagecache imagecache-'. $presetname);
   }
-  if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
-    $attributes['width'] = $image['width'];
-    $attributes['height'] = $image['height'];
+ 
+  $download_mode = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
+  
+  // checks whether the cache image already exists or not
+  $imagecache_path = imagecache_create_path($presetname, $path);
+  if ($download_mode == FILE_DOWNLOADS_PUBLIC_REPOSITORY && !file_exists($imagecache_path)) {
+    // we will create and deliver the image using the original URL, i.e. the Drupal path used by ImageCache to generate the file for a given preset and path
+    $path = _imagecache_strip_file_directory($path);
+    if (module_exists('transliteration')) {
+      $path = transliteration_get($path);
+    }
+    $imagecache_url = sprintf(
+      '%s/imagecache/%s/%s?%s',
+      $GLOBALS['base_url'],
+      $presetname,
+      $path,
+      time()
+    );
+  }
+  else {
+    if ($getsize && ($image = image_get_info($imagecache_path))) {
+      $attributes['width'] = $image['width'];
+      $attributes['height'] = $image['height'];
+    }
+  
+    $imagecache_url = imagecache_create_url($presetname, $path);
   }
-
+  
   $attributes = drupal_attributes($attributes);
-  $imagecache_url = imagecache_create_url($presetname, $path);
   return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
 }
 
