Index: imagemagick.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagemagick/imagemagick.module,v
retrieving revision 1.12
diff -u -p -r1.12 imagemagick.module
--- imagemagick.module	30 Jan 2011 21:03:42 -0000	1.12
+++ imagemagick.module	30 Jan 2011 22:30:23 -0000
@@ -47,7 +47,7 @@ function image_imagemagick_settings() {
   $form['imagemagick']['imagemagick_convert'] = array(
     '#type' => 'textfield',
     '#title' => t('Path to the "convert" binary'),
-    '#default_value' => variable_get('imagemagick_convert', '/usr/bin/convert'),
+    '#default_value' => variable_get('imagemagick_convert', 'convert'),
     '#required' => TRUE,
     '#element_validate' => array('imagemagick_element_validate_path'),
     '#description' => t('The complete path and filename of the ImageMagick <kbd>convert</kbd> binary. For example: <kbd>/usr/bin/convert</kbd> or <kbd>C:\Program Files\ImageMagick-6.3.4-Q16\convert.exe</kbd>'),
@@ -81,8 +81,11 @@ function imagemagick_element_validate_qu
  * Form element validation handler for convert executable path setting.
  */
 function imagemagick_element_validate_path($element, &$form_state) {
-  if ($form_state['values']['image_toolkit'] == 'imagemagick' && ($errors = _imagemagick_check_path($element['#value']))) {
-    form_error($element, implode('<br />', $errors));
+  if ($form_state['values']['image_toolkit'] == 'imagemagick') {
+    $form_state['imagemagick_errors'] = _imagemagick_check_path($element['#value']);
+    if ($form_state['imagemagick_errors']) {
+      form_error($element, implode('<br />', $form_state['imagemagick_errors']));
+    }
   }
 }
 
@@ -90,10 +93,15 @@ function imagemagick_element_validate_pa
  * #after_build callback to output ImageMagick version or any errors in image toolkit settings form.
  */
 function _imagemagick_build_version($element, &$form_state) {
-  // make sure path is set and valid before running after build.
-  if ($path_errors = _imagemagick_check_path($form_state['values']['imagemagick_convert'])) {
-    // check here is primarily for pre-existing bad settings...
-    // the #validate should prevent users from adding bad paths.
+  // Do not attempt to output version information when the form is submitted.
+  // imagemagick_element_validate_path() performs the validation already.
+  if ($form_state['process_input']) {
+    return $element;
+  }
+  if (!isset($form_state['imagemagick_errors'])) {
+    $form_state['imagemagick_errors'] = _imagemagick_check_path($form_state['values']['imagemagick_convert']);
+  }
+  if ($form_state['imagemagick_errors']) {
     $element['version'] = array(
       '#markup' => '<p class="error">' . implode('<br />', $path_errors) . '</p>',
     );
@@ -111,21 +119,38 @@ function _imagemagick_build_version($ele
 }
 
 /**
- * Verify ImageMagick installation path.
+ * Verify file path of ImageMagick convert binary.
+ *
+ * @param $file
+ *   The user-submitted file path to the convert binary.
  *
  * @return
- *   A list of errors indicating whether ImageMagick could not be found on this machine.
+ *   A list of errors indicating whether ImageMagick could not be found on this
+ *   machine, which is empty if it was found.
  */
-function _imagemagick_check_path($path) {
+function _imagemagick_check_path($file) {
   $errors = array();
-  if (!is_file($path)) {
-    $errors[] = t('The specified ImageMagick path %file does not exist.', array('%file' => $path));
+
+  // If only the name of the executable is given, check whether it is in the
+  // path and can be invoked.
+  if ($file == 'convert' && dirname($file) === '.') {
+    $status = _imagemagick_convert_exec('-version', $output, $errors, $file);
+    if ($status === 0) {
+      return $errors;
+    }
+  }
+
+  if (!is_file($file)) {
+    $errors[] = t('The specified ImageMagick file path %file does not exist.', array('%file' => $file));
   }
-  if (!$errors && !is_executable($path)) {
-    $errors[] = t('The specified ImageMagick path %file is not executable.', array('%file' => $path));
+  if (!$errors && !is_executable($file)) {
+    $errors[] = t('The specified ImageMagick file path %file is not executable.', array('%file' => $file));
   }
-  if ($errors && $open_basedir = ini_get('open_basedir')) {
-    $errors[] = t("PHP's <a href='!open-basedir'>open_basedir</a> security restriction is set to %open-basedir, which may be interfering with attempts to locate ImageMagick.", array('%file' => $path, '%open-basedir' => $open_basedir, '!info-link' => url('http://php.net/features.safe-mode#ini.open-basedir')));
+  if ($errors && ($open_basedir = ini_get('open_basedir'))) {
+    $errors[] = t('The PHP <a href="@php-url">open_basedir</a> security restriction is set to %open-basedir, which may prevent to locate ImageMagick.', array(
+      '%open-basedir' => $open_basedir,
+      '@php-url' => 'http://php.net/manual/en/ini.core.php#ini.open-basedir',
+    ));
   }
   return $errors;
 }
@@ -354,26 +379,48 @@ function _imagemagick_convert($source, $
   // See http://www.imagemagick.org/Usage/basics/#cmdline for more info.
   $command = escapeshellarg($source) . ' ' . implode(' ', $args) . ' ' . escapeshellarg($dest);
 
-  if (0 != _imagemagick_convert_exec($command, $output, $errors)) {
+  $status = _imagemagick_convert_exec($command, $output, $errors);
+  if ($status !== 0) {
     return FALSE;
   }
   return file_exists($dest);
 }
 
-function _imagemagick_convert_exec($command_args, &$output, &$errors) {
-  $convert_path = variable_get('imagemagick_convert', '/usr/bin/convert');
-  if ($errors = _imagemagick_check_path($convert_path)) {
-    watchdog('imagemagick', '!errors', array('!errors' => implode('<br />', $errors)), WATCHDOG_ERROR);
-    return FALSE;
+/**
+ * Executes the ImageMagick convert executable as shell command.
+ *
+ * @param $command_args
+ *   A string containing arguments to pass to the convert command, which must
+ *   have been passed through escapeshellarg() already.
+ * @param $output
+ *   (optional) A variable to assign the shell stdout to, passed by reference.
+ * @param $errors
+ *   (optional) A variable to assign the shell stderr to, passed by reference.
+ * @param $convert_path
+ *   (optional) A custom file path to the convert binary. Internal use only.
+ *
+ * @return
+ *   0 (zero) if the command succeeded, Boolean FALSE if the shell process could
+ *   not be executed, or the error status code returned by the executable.
+ */
+function _imagemagick_convert_exec($command_args, &$output = NULL, &$errors = NULL, $convert_path = NULL) {
+  if (!isset($convert_path)) {
+    // By using a default to NULL, we force users to setup the toolkit through
+    // the image toolkit administration UI. Sites enforcing a path via
+    // settings.php should know what they are doing.
+    $convert_path = variable_get('imagemagick_convert', NULL);
+    if (!isset($convert_path)) {
+      return FALSE;
+    }
   }
 
-  // Specify Drupal's root as the working a working directory so that relative
-  // paths are interpreted correctly.
+  // Use Drupal's root as working directory to resolve relative paths correctly.
   $drupal_path = DRUPAL_ROOT;
 
   if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
-    // Use Window's start command to avoid the "black window" from showing up:
-    // http://us3.php.net/manual/en/function.exec.php#56599
+    // Use Window's start command with the /B flag to make the process run in
+    // the background and avoid a shell command line window from showing up.
+    // @see http://us3.php.net/manual/en/function.exec.php#56599
     // Use /D to run the command from PHP's current working directory so the
     // file paths don't have to be absolute.
     $convert_path = 'start "ImageMagick" /D ' . escapeshellarg($drupal_path) . ' /B ' . escapeshellarg($convert_path);
