? .cache
? .project
? .projectOptions
? misc/Thumbs.db
? misc/farbtastic/Thumbs.db
? modules/imagemagick
? sites/all/modules
? sites/default/settings.php
--- modules/imagemagick/imagemagick.info
+++ modules/imagemagick/imagemagick.info
@@ -0,0 +1,4 @@
+; $Id: $
+name = ImageMagick
+description = ImageMagick image toolkit.
+core = 6.x

--- modules/imagemagick/imagemagick.module
+++ modules/imagemagick/imagemagick.module
@@ -0,0 +1,146 @@
+<?php
+// $Id: $
+
+/**
+ * @file
+ * ImageMagick toolkit functions
+ */
+
+/**
+ * Settings form for the toolkit.
+ */
+function imagemagick_settings_form() {
+  $form['#after_build'][] = '_imagemagick_version';
+  $form['#validate'][] = '_imagemagick_validate';
+  $form['image_imagemagick_convert'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Path to the "convert" binary'),
+    '#default_value' => variable_get('image_imagemagick_convert', '/usr/bin/convert'),
+    '#required' => TRUE,
+  );
+  return system_settings_form($form);
+}
+
+function _imagemagick_version(&$form, &$form_state) {
+  $path = realpath($form_state['values']['image_imagemagick_convert']);
+  if (_imagemagick_check_path($path)) {
+    _imagemagick_convert_exec($path, '-version', $output, $errors);
+    $form['image_imagemagick_version'] = array(
+      '#type' => 'item',
+      '#value' => '<pre>'. check_plain($output) .'</pre>',
+    );
+    $form['buttons']['#weight'] = 10;
+  }
+  else {
+    _imagemagick_validate($form, $form_state);
+  }
+  return $form;
+}
+
+function _imagemagick_validate(&$form, &$form_state) {
+  $path = realpath($form_state['values']['image_imagemagick_convert']);
+  if (!_imagemagick_check_path($path)) {
+    if (ini_get('open_basedir')) {
+      form_set_error('image_imagemagick_convert', t("No file named %file could be found. PHP's <a href='@open-basedir'>open_basedir</a> security resriction may be interfearing with the attempts to locate it.", array('%file' => $path, '@open-basedir' => url('http://us2.php.net/features.safe-mode') )));
+    }
+    else {
+      form_set_error('image_imagemagick_convert', t('The specified ImageMagic path %file does not exist.', array('%file' => $path)));
+    }
+  }
+  return $form;
+}
+
+/**
+ * Simple check if the path given is a file. 
+ */
+function _imagemagick_check_path($path) {
+  return is_file($path);
+}
+
+/**
+ * Implementation of hook_image_toolkit().
+ */
+function imagemagick_image_toolkit($op, $source = NULL, $destination = NULL, $params = array()) {
+  $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert');
+  if (!is_file($convert_path)) {
+    drupal_set_message(t("ImageMagick could not be found. The admin will need to set the path on the <a href='!settings'>image toolkit settings</a> page.", array('!settings' => url('admin/settings/imagemagick'))), 'error');
+    return FALSE;
+  }
+
+  switch ($op) {
+    case 'crop':
+      $filter = ' -crop '. intval($params['width']) .'x'. intval($params['height']) .'+'. intval($params['x']) .'+'. intval($params['y']);
+      break;
+      
+    case 'resize':
+      $filter = ' -scale '. intval($params['width']) .'x'. intval($params['height']) .'! -filter QUADRATIC';
+      break;
+      
+    case 'rotate':
+      $filter = ' -rotate '. intval($params['degrees']) .' -background #'. str_pad(dechex($params['bg_color']), 6, 0);
+      break;
+    
+    default:
+      return FALSE;
+  }
+
+  $args = $filter .' '. escapeshellarg($source) .' '. escapeshellarg($destination);
+  if (0 != _imagemagick_convert_exec($convert_path, $args, $output, $errors)) {
+    return FALSE;
+  }
+  return file_exists($destination);
+}
+
+/**
+ * Execute ImageMagick
+ * 
+ * @param $convert_path 
+ *   Full path to the convert binary.
+ * @param $command_args
+ *   String with parameters to pass to convert. This should be properly escaped 
+ *   with escapeshellarg().
+ * @param $output
+ *   Output string.
+ * @param $errors
+ *   Error string.
+ * @return
+ *   Exit code from the convert binary or FALSE on error. 
+ */
+function _imagemagick_convert_exec($convert_path, $command_args, &$output, &$errors) { 
+  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 /D to run the command from PHP's current working directory so the
+    // file paths don't have to be absolute.
+    $convert_path = 'start "window title" /D'. getcwd() .' /b '. escapeshellarg($convert_path);
+  }
+
+  $descriptors = array(
+    0 => array('pipe', 'r'), // stdin
+    1 => array('pipe', 'w'), // stdout
+    2 => array('pipe', 'w')  // stderr
+  );
+  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 (!empty($errors)) {
+      drupal_set_message(t("ImageMagick reported an error: %error", array('%error' => $errors)), 'error');
+    }
+
+    fclose($pipes[0]);
+    fclose($pipes[1]);
+    fclose($pipes[2]);
+    return proc_close($h);
+  }
+  return FALSE;
+}

