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	15 Sep 2009 12:06:35 -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);
       }
     }
   }
@@ -603,9 +603,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 +839,27 @@ function _image_build_derivatives($node,
     $images[$key] = $original_path;
   }
 
+  // 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;
+  }
+      
   // 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 +1002,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.
+  if (!empty($origname)) {
+    $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,7 +1027,7 @@ 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'],
@@ -1095,6 +1126,9 @@ function image_create_node_from($filepat
   $node->new_file = TRUE;
   $node->images[IMAGE_ORIGINAL] = $filepath;
 
+  // Ensure derivatives are built before node_save() in case Filefield_paths is installed.
+  $node->images = _image_build_derivatives($node, FALSE);
+
   // Save the node.
   $node = node_submit($node);
   node_save($node);
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;
