Index: image.imagemagick.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.imagemagick.inc,v
retrieving revision 1.6
diff -u -r1.6 image.imagemagick.inc
--- image.imagemagick.inc	20 Mar 2007 17:18:35 -0000	1.6
+++ image.imagemagick.inc	28 Mar 2007 21:25:17 -0000
@@ -12,23 +12,37 @@
  * Validate and return toolkit specific settings
  */
 function image_imagemagick_settings() {
+  $form['#after_build'] = array('_image_imagemagick_build_version');
   $form['image_imagemagick_convert'] = array(
     '#type' => 'textfield',
-    '#title' => t('Location of the "convert" binary'),
+    '#title' => t('Path to the "convert" binary'),
     '#default_value' => variable_get('image_imagemagick_convert', '/usr/bin/convert'),
-    '#size' => 64,
     '#required' => TRUE,
-    '#validate' => array('image_imagemagick_valid_file' => array('image_imagemagick_convert')),
   );
   return $form;
 }
 
-function image_imagemagick_valid_file($formelement = NULL, $fieldname = NULL) {
-  $convert_path = $formelement['#value'];
+function _image_imagemagick_build_version($form, $form_element) {
+  $valid = _image_imagemagick_check_path($form_element['image_imagemagick_convert'], 'image_imagemagick_convert');
+  if ($valid) {
+    _image_imagemagick_convert_exec('-version', $output, $errors);
+    $form['image_imagemagick_version'] = array(
+      '#type' => 'item',
+      '#title' => t('Version'),
+      '#value' => '<pre>'. check_plain($output) .'</pre>',
+    );
+  }
+  return $form;
+}
 
-  if (!is_file($convert_path)) {
-    form_set_error($fieldname, t('%file does not exist.', array('%file' => $convert_path)));
+function _image_imagemagick_check_path($path, $attach_error_to = FALSE) {
+  if (is_file($path)) {
+    return TRUE;
   }
+  if ($attach_error_to) {
+    form_set_error($attach_error_to, t('The specified ImageMagic path %file does not exist.', array('%file' => $path)));
+  }
+  return FALSE;
 }
 
 /**
@@ -43,8 +57,8 @@
  * Rotate an image
  */
 function image_imagemagick_rotate($source, $dest, $degrees) {
-  $filter = ' -rotate '. escapeshellarg($degrees) .' -background #000000';  
-  return _image_imagemagick_convert($source, $dest, $filter);  
+  $filter = ' -rotate '. escapeshellarg($degrees) .' -background #000000';
+  return _image_imagemagick_convert($source, $dest, $filter);
 }
 
 /**
@@ -52,15 +66,29 @@
  */
 function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
   $filter = ' -crop '. $width .'x'. $height .'+'. $x .'+'. $y;
-  return _image_imagemagick_convert($source, $dest, $filter);  
+  return _image_imagemagick_convert($source, $dest, $filter);
 }
 
 /**
  * Calls the convert executable with the specified filter
  */
 function _image_imagemagick_convert($source, $dest, $filter) {
+  $command = implode(' ', array(
+    preg_replace("/[^A-Za-z0-9\!\.\-\+\040]/", '', $filter),
+    escapeshellarg($source),
+    escapeshellarg($dest),
+  ));
+
+  if (0 != _image_imagemagick_convert_exec($command, $output, $errors)) {
+    return FALSE;
+  }
+  return file_exists($dest);
+}
+
+function _image_imagemagick_convert_exec($command_args, &$output, &$errors) {
   $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert');
-  if (!file_exists($convert_path)) {
+  if (!_image_imagemagick_check_path($convert_path)) {
+    drupal_set_message(t("ImageMagick could not be found. The admin will need to set the path on the <a href='@image-toolkit-settings'>image toolkit page</a>.", array('@image-toolkit-settings' => url('admin/settings/image-toolkit'))), 'error');
     return FALSE;
   }
 
@@ -72,36 +100,28 @@
     $convert_path = 'start "window title" /D'. getcwd() .' /b '. escapeshellarg($convert_path);
   }
 
-  $command = implode(' ', array(
-    $convert_path,
-    preg_replace("/[^A-Za-z0-9\!\.\-\+\040]/", '', $filter),
-    escapeshellarg($source),
-    escapeshellarg($dest),
-  ));
-
-
-  $status = _image_imagemagick_exec($command, $output, $error);
-  if ($error) {
-    drupal_set_message("ImageMagick reported an error: $error");
-    #drupal_set_message("ImageMagick command: $command");
-    #drupal_set_message("ImageMagick output: $output");
-  }
-
-  if ($status != 0) {
-    return FALSE;
-  }
-  return file_exists($dest);
-}
-
-function _image_imagemagick_exec($command, &$output, &$errors) {
   $descriptors = array(
     0 => array('pipe', 'r'), // stdin
     1 => array('pipe', 'w'), // stdout
     2 => array('pipe', 'w')  // stderr
   );
-  if ($h = proc_open($command, $descriptors, $pipes)) {
-    $output = fgets($pipes[1]);
-    $errors = fgets($pipes[2]);
+  if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes)) {
+    $output = '';
+    while (!feof($pipes[1])) {
+      $output .= fgets($pipes[1]);
+    }
+
+    $errors = '';
+    while (!feof($pipes[2])) {
+      $errors .= fgets($pipes[2]);
+    }
+
+    #drupal_set_message(t("ImageMagick command: %command", array('%command' => $convert_path .' '. $command_args)));
+    #drupal_set_message(t("ImageMagick output: %output", array('%output' => $output)));
+    if ($errors) {
+      drupal_set_message(t("ImageMagick reported an error: %error", array('%error' => $errors)), 'error');
+    }
+
     fclose($pipes[0]);
     fclose($pipes[1]);
     fclose($pipes[2]);

