Index: image.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.install,v
retrieving revision 1.33
diff -u -p -r1.33 image.install
--- image.install	5 Sep 2009 15:12:00 -0000	1.33
+++ image.install	14 Sep 2009 09:45:55 -0000
@@ -376,3 +376,18 @@ function image_update_6103() {
   }
   return $ret;
 }
+
+/**
+ * Copy basename of filepath to filename field in files table.
+ */
+function image_update_6104() {
+  $ret = array();
+  $result = db_query("SELECT f.fid, f.filepath FROM {files} f JOIN {image} i ON i.fid = f.fid");
+  while ($filedetails = db_fetch_object($result)) {
+    db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d",
+        basename($filedetails->filepath),
+        $filedetails->fid
+    );
+  }
+  return $ret;
+}
Index: image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v
retrieving revision 1.322
diff -u -p -r1.322 image.module
--- image.module	13 Sep 2009 20:21:04 -0000	1.322
+++ image.module	7 Oct 2009 14:15:37 -0000
@@ -172,7 +172,7 @@ function image_operations_rebuild($nids)
     if ($node = node_load($nid)) {
       if ($node->type == 'image') {
         $node->rebuild_images = TRUE;
-        image_update($node);
+        node_save($node);
       }
     }
   }
@@ -522,6 +522,10 @@ function _image_build_derivatives_if_nee
 
   // Correct any problems with the derivative images.
   if ($node->rebuild_images) {
+    // Identify node as being in the process of loading so derivative filenames must be
+    // re-created from the current name of ORIGINAL even if Filefield_paths is installed,
+    // because the Filefield_paths hooks will not be invoked by image_update().
+    $node->load_in_progress = TRUE;
     // Possibly TODO: calling a hook implementation here isn't very elegant.
     // but the code there is tangled and it works, so maybe leave it ;)    
     image_update($node);
@@ -603,9 +607,6 @@ function image_update(&$node) {
       }
 
       $node->images = _image_build_derivatives($node, FALSE);
-
-      // Prevent multiple rebuilds.
-      $node->rebuild_images = FALSE;
     }
 
     $sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
@@ -842,10 +843,28 @@ function _image_build_derivatives($node,
     $images[$key] = $original_path;
   }
 
+  // Retrieve original name if Filefield_paths installed and there is not a node load in progress.
+  $origname = '';
+
+  if (module_exists('filefield_paths') && empty($node->load_in_progress)) {
+      // Find the original image.
+      $original_file = db_fetch_object(db_query("SELECT f.origname FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND i.image_size = '%s'", $node->nid, IMAGE_ORIGINAL));
+      $origname = $original_file->origname;
+  }
+      
   // Resize for the necessary sizes.
   $image_info = image_get_info($original_path);
+
+  // Pass original name if Filefield_paths installed.
+  if (!empty($origname)) {
+    $origname_path = dirname($original_path) .'/'. $origname;
+  }
+  else {
+    $origname_path = $original_path;
+  }
+
   foreach ($needed_sizes as $key => $size) {
-    $destination = _image_filename($original_path, $key, $temp);
+    $destination = _image_filename($origname_path, $key, $temp);
 
     $status = FALSE;
     switch ($size['operation']) {
@@ -988,7 +1007,24 @@ function _image_is_required_size($size) 
  */
 function _image_insert(&$node, $size, $image_path) {
   $original_path = $node->images[IMAGE_ORIGINAL];
-  if (file_move($image_path, _image_filename($original_path, $size))) {
+
+  // Retrieve original name if Filefield_paths installed.
+  $origname = '';
+  if (module_exists('filefield_paths')) {
+      // Find the original image.
+      $original_file = db_fetch_object(db_query("SELECT f.origname FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND i.image_size = '%s'", $node->nid, IMAGE_ORIGINAL));
+      $origname = $original_file->origname;
+  }
+      
+  // Pass original name if Filefield_paths installed and not creating derivatives during node load..
+  if (!empty($origname) && empty($node->load_in_progress)) {
+    $origname_path = dirname($original_path) .'/'. $origname;
+  }
+  else {
+    $origname_path = $original_path;
+  }
+
+  if (file_move($image_path, _image_filename($origname_path, $size))) {
     // Update the node to reflect the actual filename, it may have been changed
     // if a file of the same name already existed.
     $node->images[$size] = $image_path;
@@ -996,13 +1032,19 @@ function _image_insert(&$node, $size, $i
     $image_info = image_get_info($image_path);
     $file = array(
       'uid' => $node->uid,
-      'filename' => $size,
+      'filename' => basename($image_path),
       'filepath' => $image_path,
       'filemime' => $image_info['mime_type'],
       'filesize' => $image_info['file_size'],
       'status' => FILE_STATUS_PERMANENT,
       'timestamp' => time(),
     );
+
+    // Add original name to derivatives only if Filefield_paths installed and rebuilding derivatives during image node load.
+    if (module_exists('filefield_paths') && !empty($origname) && $size != IMAGE_ORIGINAL && $node->load_in_progress) {
+      $file['origname'] = basename(_image_filename($origname, $size));
+    }
+
     drupal_write_record('files', $file);
     $image = array(
       'fid' => $file['fid'],
Index: contrib/image_gallery/image_gallery.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_gallery/image_gallery.pages.inc,v
retrieving revision 1.1
diff -u -p -r1.1 image_gallery.pages.inc
--- contrib/image_gallery/image_gallery.pages.inc	4 Sep 2009 13:24:41 -0000	1.1
+++ contrib/image_gallery/image_gallery.pages.inc	13 Sep 2009 16:44:40 -0000
@@ -26,7 +26,7 @@ function image_gallery_page($type = NULL
 
     case IMAGE_GALLERY_SORT_FILENAME:
       $join = "INNER JOIN {image} i ON n.nid = i.nid INNER JOIN {files} f ON i.fid = f.fid";
-      $where = "AND f.filename = '%s'";
+      $where = "AND i.image_size = '%s'";
       $args[] = IMAGE_ORIGINAL;
       $order = "ORDER BY n.sticky DESC, f.filepath ASC";
       break;
