Index: imagefield.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.install,v
retrieving revision 1.26
diff -u -r1.26 imagefield.install
--- imagefield.install	14 Mar 2009 23:51:09 -0000	1.26
+++ imagefield.install	15 Mar 2009 04:41:40 -0000
@@ -179,7 +179,7 @@
   $col_data = $field['field_name'] .'_data';
   $col_list = $field['field_name'] .'_list';
 
-  $limit = 10;
+  $limit = 100;
   $result = db_query_range("SELECT * FROM {". $table ."} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_node'], 0, $limit);
   $has_processed = FALSE;
 
@@ -213,14 +213,9 @@
     // Serialize it and store it back to the db.
     db_query($query, serialize($data), $list, $row['vid'], $row['delta']);
 
-    // Create the thumbnail for that image.
-    $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $row[$col_fid]));
-    if ($file && file_exists($file->filepath) && !file_exists(imagefield_file_admin_thumb_path($file))) {
-      imagefield_file_insert($file);
-    }
-
     // Previous updates generated a thumbnail in the wrong location, delete
     // these thumbnails if present.
+    $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $row[$col_fid]));
     if ($file && file_exists($file->filepath . '.thumb.jpg')) {
       unlink($file->filepath . '.thumb.jpg');
     }
@@ -254,3 +249,29 @@
 
   return $ret;
 }
+
+
+/**
+ * Delete thumbnails spread throughout the files directory.
+ */
+function imagefield_update_6005() {
+  $ret = array();
+
+  $result = db_query("SELECT * FROM {files} WHERE filemime LIKE 'image/%'");
+
+  while ($file = db_fetch_object($result)) {
+    $thumb_path_a = $file->filepath . '.thumb.jpg';
+
+    $extension_dot = strrpos($file->filepath, '.');
+    $extension = substr($file->filepath, $extension_dot + 1);
+    $basepath = substr($file->filepath, 0, $extension_dot);
+    $thumb_path_b = $basepath .'.thumb.'. $extension;
+  
+    file_delete($thumb_path_a);
+    file_delete($thumb_path_b);
+  }
+
+  $ret[] = array('success' => TRUE, 'query' => t('Deleted admin thumbnails distributed throughout files directory. All thumbnails are now stored in the "imagefield_thumbs" directory.'));
+
+  return $ret;
+}
Index: imagefield_file.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield_file.inc,v
retrieving revision 1.9
diff -u -r1.9 imagefield_file.inc
--- imagefield_file.inc	11 Mar 2009 01:41:57 -0000	1.9
+++ imagefield_file.inc	15 Mar 2009 04:41:40 -0000
@@ -22,20 +22,9 @@
 
 /**
  * Implementation of hook_file_insert().
- *
- * Create an image thumbnail to be used as a preview when editing a node.
  */
 function imagefield_file_insert($file) {
-  if (imagefield_file_is_image($file)) {
-    $info = image_get_info($file->filepath);
-    if ($info['width'] <= 100 || $info['height'] <= 100) {
-      $newfile = drupal_clone($file);
-      file_copy($newfile->filepath, imagefield_file_admin_thumb_path($newfile));
-    }
-    else {
-      image_scale($file->filepath, imagefield_file_admin_thumb_path($file), 100, 100);
-    }
-  }
+  // Currently empty. Thumbnails are now generated on the fly.
 }
 
 /**
@@ -45,7 +34,7 @@
  */
 function imagefield_file_delete($file) {
   if (imagefield_file_is_image($file)) {
-    file_delete(imagefield_file_admin_thumb_path($file));
+    file_delete(imagefield_file_admin_thumb_path($file, FALSE));
   }
 }
 
@@ -60,10 +49,53 @@
 /**
  * Given a file, return the path the image thumbnail used while editing.
  */
-function imagefield_file_admin_thumb_path($file) {
+function imagefield_file_admin_thumb_path($file, $create_thumb = TRUE) {
   $file = (object)$file;
-  $extension_dot = strrpos($file->filepath, '.');
-  $extension = substr($file->filepath, $extension_dot + 1);
-  $basepath = substr($file->filepath, 0, $extension_dot);
-  return $basepath .'.thumb.'. $extension;
+  $short_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '/', '', $file->filepath);
+  $filepath = file_directory_path() . '/imagefield_thumbs' . $short_path;
+
+  if ($create_thumb) {
+    imagefield_create_admin_thumb($file->filepath, $filepath);
+  }
+
+  return $filepath;
+}
+
+/**
+ * Create a thumbnail to be shown while editing an image.
+ */
+function imagefield_create_admin_thumb($source, $destination) {
+  if (!is_file($source)) {
+    return FALSE;
+  }
+
+  $info = image_get_info($source);
+  $size = explode('x', variable_get('imagefield_thumb_size', '100x100'));
+
+  // Check if the destination image needs to be regenerated to match a new size.
+  if (is_file($destination)) {
+    $thumb_info = image_get_info($destination);
+    if ($thumb_info['width'] != $size[0] && $thumb_info['height'] != $size[1] && ($info['width'] > $size[0] || $info['height'] > $size[1])) {
+      unlink($destination);
+    }
+    else {
+      return;
+    }
+  }
+
+  // Ensure the destination directory exists and is writable.
+  $directories = explode('/', $destination);
+  array_pop($directories); // Remove the file itself.
+  foreach ($directories as $directory) {
+    $full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;
+    file_check_directory($full_path, FILE_CREATE_DIRECTORY);
+  }
+
+  // Create the thumbnail.
+  if ($info['width'] <= $size[0] || $info['height'] <= $size[1]) {
+    file_copy($source, $destination);
+  }
+  else {
+    image_scale($source, $destination, $size[0], $size[1]);
+  }
 }
