--- c:/DOCUME~1/DOCTOR~1/LOCALS~1/Temp/ediff29127Dd2912iiv	Wed Jun 13 15:00:22 2007
+++ c:/DOCUME~1/DOCTOR~1/LOCALS~1/Temp/imagecache.module	Wed Jun 13 15:00:22 2007
@@ -13,7 +13,7 @@
   
   $get[q] = files/imagecache/<preset>/path
 
-	@todo: improve image resizing, reduce distortion.
+  @todo: improve image resizing, reduce distortion.
   @todo: add watermarking capabilities.
   @todo: split action/handlers into their own little .inc files.
   @todo: enforce permissions.
@@ -127,76 +127,89 @@
 
   $destination = file_create_path() . '/imagecache/'. $preset .'/'. $path;
 
-  // prepend presetname to tmp file name to prevent namespace clashes when
-  // multiple presets for the same image are called in rapid succession.
-  $tmpdestination = file_directory_temp() . '/' . $preset . str_replace(dirname($path) . '/', '', $path);
-
-  $dir = dirname($destination);
-  //drupal_set_message('$tmpdestination: '. $tmpdestination);
-  //drupal_set_message('$destination: '. $destination);
-  if(!file_check_directory($dir)) {
-    $folders = explode("/",$dir);
-   
-    foreach($folders as $folder) {
-      $tpath[] = $folder;
-      if(!file_check_directory(implode("/", $tpath))) {
-        $p = implode("/", $tpath);
-        if (!file_exists($p)) {
-          mkdir($p);
-        }
-      }
-    }
-  }
-
-  if (!file_check_directory($dir)) {
-    watchdog('imagecache', t('Could not create destination: %dir', array('%dir' => $destination)), WATCHDOG_ERROR);
-    return;
-  }
-
-  //check if file exists to prevent multiple apache children from trying to generate.
-  if (!is_file($tmpdestination)) {
+  // For mod_rewrite under IIS, we can't have the same rule rewriting
+  // existing and non-existing imagecache images, so we're going to have to
+  // send all requests through here, and return the pre-created image if
+  // it exists.
+  $destination_physical = $_SERVER['DOCUMENT_ROOT'] . '/' . $destination;
+  if (is_file($destination_physical)) {
+    // We have previously generated this file
     $generated = TRUE;
-    file_copy($source, $tmpdestination);
-
-    foreach($actions as $action) {
-      $size = getimagesize($tmpdestination);
-      $new_width = _imagecache_filter('width', $action['data']['width'], $size[0], $size[1]);
-      $new_height = _imagecache_filter('height', $action['data']['height'], $size[0], $size[1]);
-      foreach($action['data'] as $key => $value) {
-        $action['data'][$key] = _imagecache_filter($key, $value, $size[0], $size[1], $new_width, $new_height);
-      }
-      switch($action['data']['function']) {
-        case 'resize':
-          if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) {
-            watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
-          }
-          break;
-      
-        case 'scale':
-          if ($action['data']['fit'] == 'outside' && $action['data']['width'] && $action['data']['height']) {
-            $ratio = $size[0]/$size[1];
-            $new_ratio = $action['data']['width']/$action['data']['height'];
-            $action['data']['width'] = $ratio > $new_ratio ? 0 : $action['data']['width'];
-            $action['data']['height'] = $ratio < $new_ratio ? 0 : $action['data']['height'];
-          }
-          // set impossibly large values if the width and height aren't set.
-          $action['data']['width'] = $action['data']['width'] ? $action['data']['width'] : 9999999;
-          $action['data']['height'] = $action['data']['height'] ? $action['data']['height'] : 9999999;
-          if (!image_scale($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) {
-            watchdog('imagecache', t('Imagecache scale action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
-          }
-          break;
+  }
+  else {
+    // prepend presetname to tmp file name to prevent namespace clashes when
+    // multiple presets for the same image are called in rapid succession.
+    $tmpdestination = file_directory_temp() . '/' . $preset . str_replace(dirname($path) . '/', '', $path);
+  
+    // Generate the file...
 
-        case 'crop':
-          if (!image_crop($tmpdestination, $tmpdestination, $action['data']['xoffset'] , $action['data']['yoffset'],$action['data']['width'],$action['data']['height'])) {
-            watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
+    $dir = dirname($destination);
+    //drupal_set_message('$tmpdestination: '. $tmpdestination);
+    //drupal_set_message('$destination: '. $destination);
+    if(!file_check_directory($dir)) {
+      $folders = explode("/",$dir);
+     
+      foreach($folders as $folder) {
+        $tpath[] = $folder;
+        if(!file_check_directory(implode("/", $tpath))) {
+          $p = implode("/", $tpath);
+          if (!file_exists($p)) {
+            mkdir($p);
           }
-          break;
+        }
       }
     }
-    file_move($tmpdestination, $destination);
-  } else {
-    $generated = TRUE;
+  
+    if (!file_check_directory($dir)) {
+      watchdog('imagecache', t('Could not create destination: %dir', array('%dir' => $destination)), WATCHDOG_ERROR);
+      return;
+    }
+  
+    //check if file exists to prevent multiple apache children from trying to generate.
+    if (!is_file($tmpdestination)) {
+      $generated = TRUE;
+      file_copy($source, $tmpdestination);
+  
+      foreach($actions as $action) {
+        $size = getimagesize($tmpdestination);
+        $new_width = _imagecache_filter('width', $action['data']['width'], $size[0], $size[1]);
+        $new_height = _imagecache_filter('height', $action['data']['height'], $size[0], $size[1]);
+        foreach($action['data'] as $key => $value) {
+          $action['data'][$key] = _imagecache_filter($key, $value, $size[0], $size[1], $new_width, $new_height);
+        }
+        switch($action['data']['function']) {
+          case 'resize':
+            if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) {
+              watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
+            }
+            break;
+        
+          case 'scale':
+            if ($action['data']['fit'] == 'outside' && $action['data']['width'] && $action['data']['height']) {
+              $ratio = $size[0]/$size[1];
+              $new_ratio = $action['data']['width']/$action['data']['height'];
+              $action['data']['width'] = $ratio > $new_ratio ? 0 : $action['data']['width'];
+              $action['data']['height'] = $ratio < $new_ratio ? 0 : $action['data']['height'];
+            }
+            // set impossibly large values if the width and height aren't set.
+            $action['data']['width'] = $action['data']['width'] ? $action['data']['width'] : 9999999;
+            $action['data']['height'] = $action['data']['height'] ? $action['data']['height'] : 9999999;
+            if (!image_scale($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) {
+              watchdog('imagecache', t('Imagecache scale action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
+            }
+            break;
+  
+          case 'crop':
+            if (!image_crop($tmpdestination, $tmpdestination, $action['data']['xoffset'] , $action['data']['yoffset'],$action['data']['width'],$action['data']['height'])) {
+              watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
+            }
+            break;
+        }
+      }
+      file_move($tmpdestination, $destination);
+    } else {
+      $generated = TRUE;
+    }
   }
 
   if ($generated)  {
