Index: imagefield_import.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield_import/imagefield_import.module,v
retrieving revision 1.8.2.5
diff -u -p -r1.8.2.5 imagefield_import.module
--- imagefield_import.module	19 Apr 2009 20:11:40 -0000	1.8.2.5
+++ imagefield_import.module	20 Apr 2009 04:11:46 -0000
@@ -163,32 +163,16 @@ function imagefield_import_form_submit($
   if ($op == t('Import')) {
     $source_path = variable_get('imagefield_import_source_path', '');
     if (file_check_directory($source_path)) {
-      // determine the node type, and the target fieldname to import into
-      $targetsetting = variable_get('imagefield_import_fieldname', FALSE);
-      list($node_type, $field) = split(":::", $targetsetting);
-      // try to avoid php's script timeout with a bunch of large files or a slow machine
-      if (!ini_get('safe_mode'))
-      set_time_limit(0);
-      foreach (array_filter($form_state['values']['import_file']) as $index) {
-        // Check the file is OK
-        $filename = $form_state['values']['file_list'][$index];
-        $source_filepath = file_check_location($source_path .'/'. $filename, $source_path);
-        if ($source_filepath and file_validate_is_image($source_filepath)) {
-          $file_info = image_get_info($source_filepath);
-          $file_info['filepath'] = $source_filepath;
-          // This is follwoing the method suggested by: http://drupal.org/node/292904 ...
-          $file = array($field => $file_info);
-          $title = $form_state['values']['title'][$index];
-          $caption = $form_state['values']['body'][$index];
-          $taxonomy = $form_state['values']['taxonomy'];
-          if (_imagefield_import_process_form($file, $node_type, $title, $caption, $taxonomy))
-          // delete original if it all went well.
-          file_delete($source_filepath);
-        }
-        else {
-          drupal_set_message('Problem with the file location:'. $source_filepath, 'error');
-        }
-      } // foreach( file )
+      $selected = array_filter($form_state['values']['import_file']);
+
+      $batch = array(
+        'title' => t('Importing images'),
+        'operations' => array(
+          array('_imagefield_import_batch_import', array($selected, $form_state['values']['file_list'], $source_path))
+        ),
+      );
+      batch_set($batch);
+      batch_process();
     }
     else {
       drupal_set_message('Source directory has not been set in the settings?', 'error');
@@ -196,6 +180,53 @@ function imagefield_import_form_submit($
   }
 }
 
+function _imagefield_import_batch_import($selected, $images, $source_path, &$context) {
+  if (!isset($context['sandbox']['progress'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['max'] = count($selected);
+    $context['sandbox']['images'] = $selected;
+  }
+
+  // determine the node type, and the target fieldname to import into
+  $targetsetting = variable_get('imagefield_import_fieldname', FALSE);
+  list($node_type, $field) = split(":::", $targetsetting);
+
+  // Process images by groups of 5.
+  $count = min(5, count($context['sandbox']['images']));
+  for ($i = 1; $i <= $count; $i++) {
+    $index = array_shift($context['sandbox']['images']);
+
+    // Check the file is OK
+    $filename = $images[$index];
+    $source_filepath = file_check_location($source_path .'/'. $filename, $source_path);
+    if ($source_filepath and file_validate_is_image($source_filepath)) {
+      $file_info = image_get_info($source_filepath);
+      $file_info['filepath'] = $source_filepath;
+      // This is follwoing the method suggested by: http://drupal.org/node/292904 ...
+      $file = array($field => $file_info);
+      $title = $form_state['values']['title'][$index];
+      $caption = $form_state['values']['body'][$index];
+      $taxonomy = $form_state['values']['taxonomy'];
+      // delete original if it all went well.
+      if (_imagefield_import_process_form($file, $node_type, $title, $caption, $taxonomy)) {
+        file_delete($source_filepath);
+      }
+    }
+    else {
+      drupal_set_message('Problem with the file location:'. $source_filepath, 'error');
+    }
+
+    // Update our progress information.
+    $context['sandbox']['progress']++;
+  }
+
+  // Inform the batch engine that we are not finished,
+  // and provide an estimation of the completion level we reached.
+  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
+    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  }
+}
+
 /**
  * see: http://drupal.org/node/292904
  */
