Index: image.imagemagick.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.imagemagick.inc,v
retrieving revision 1.5
diff -u -r1.5 image.imagemagick.inc
--- image.imagemagick.inc	6 Mar 2007 17:10:08 -0000	1.5
+++ image.imagemagick.inc	14 Mar 2007 22:07:07 -0000
@@ -42,8 +42,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);
 }
 
 /**
@@ -51,22 +51,7 @@
  */
 function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
   $filter = ' -crop '. $width .'x'. $height .'+'. $x .'+'. $y;
-  return _image_imagemagick_convert($source, $dest, $filter);  
-}
-
-/**
- * Helper function: Escape filename for Unix or Windows shell.
- *
- * @param $filename filename to escape -- may contain spaces or other problematic characters
- *
- * @return escaped version of $filename which should pass through shell
- */
-function _image_escape_shell($filename) {
-  if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
-    return '"' . addslashes($filename) . '"';
-  } else {
-    return escapeshellarg($filename);
-  }
+  return _image_imagemagick_convert($source, $dest, $filter);
 }
 
 /**
@@ -77,31 +62,50 @@
   if (!file_exists($convert_path)) {
     return FALSE;
   }
-  
-  $filter = preg_replace("/[^A-Za-z0-9\!\.\-\+\040]/", '', $filter);
-  $source = _image_escape_shell($source);
-  $dest = _image_escape_shell($dest);
-  $err = _image_exec("$convert_path $filter $source $dest");
-  if ($err) {
-    return FALSE;
+
+  $convert_path = escapeshellarg($convert_path);
+  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
+    $convert_path = 'start "a window title" /b '. $convert_path;
   }
-  if (!file_exists(trim($dest, "'"))) {
+
+  $command = implode(' ', array(
+    $convert_path,
+    preg_replace("/[^A-Za-z0-9\!\.\-\+\040]/", '', $arguments),
+    escapeshellarg($source),
+    escapeshellarg($dest),
+  ));
+
+
+  $status = _image_imagemagick_exec($command, $output, $error);
+  #drupal_set_message("cmd: $command");
+  #drupal_set_message("return: $status");
+  #drupal_set_message("output: $output");
+  if ($error) {
+    drupal_set_message("ImageMagick reported an error: $error");
+  }
+
+  if ($status != 0) {
     return FALSE;
   }
-  
-  return TRUE;
+  return file_exists($dest);
 }
 
-function _image_exec($cmd) {
-  if (substr(php_uname(), 0, 7) == "Windows"){
-    if ($h = popen("start \"bla\" $cmd", "r")) {
-      pclose($h);
-      return true;
-    } else {
-      return false;
-    }
-  } else {
-    return exec($cmd);    
+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 = stream_get_contents($pipes[1]);
+    $errors = stream_get_contents($pipes[2]);
+    fclose($pipes[0]);
+    fclose($pipes[1]);
+    fclose($pipes[2]);
+    return proc_close($h);
   }
-} 
+  return FALSE;
+}
 

