Index: imagemagick.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagemagick/imagemagick.module,v
retrieving revision 1.6
diff -u -p -r1.6 imagemagick.module
--- imagemagick.module	30 Jan 2011 18:48:20 -0000	1.6
+++ imagemagick.module	30 Jan 2011 18:57:21 -0000
@@ -30,12 +30,12 @@ function image_imagemagick_settings() {
   $form['imagemagick_quality'] = array(
     '#type' => 'textfield',
     '#title' => t('Image quality'),
-    '#description' => t('Define the image quality of processed images. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
     '#size' => 10,
     '#maxlength' => 3,
     '#default_value' => variable_get('imagemagick_quality', 75),
     '#field_suffix' => '%',
-    '#element_validate' => array('imagemagick_quality_element_validate'),
+    '#element_validate' => array('imagemagick_element_validate_quality'),
+    '#description' => t('Define the image quality of processed images. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
   );
 
   $form['imagemagick'] = array(
@@ -49,8 +49,8 @@ function image_imagemagick_settings() {
     '#title' => t('Path to the "convert" binary'),
     '#default_value' => variable_get('imagemagick_convert', '/usr/bin/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>'),
-    '#element_validate' => array('imagemagick_validate_path'),
   );
   // Prepare sub-element to output version or errors.
   $form['imagemagick']['version'] = array();
@@ -68,38 +68,46 @@ function image_imagemagick_settings() {
   return $form;
 }
 
-function imagemagick_quality_element_validate($element) {
+/**
+ * Form element validation handler for image quality settings field.
+ */
+function imagemagick_element_validate_quality($element, &$form_state) {
   if ($element['#value'] < 0 || $element['#value'] > 100) {
     form_error($element, t('!name must be a value between 0 and 100.', array('!name' => $element['#title'])));
   }
 }
 
-function _imagemagick_build_version($form_element, $form_state) {
+/**
+ * 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));
+  }
+}
+
+/**
+ * #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.
-    $form_element['version'] = array(
-      '#markup' => '<p class="error">'. implode('<br />', $path_errors) .'</p>',
+    $element['version'] = array(
+      '#markup' => '<p class="error">' . implode('<br />', $path_errors) . '</p>',
     );
   }
   else {
     _imagemagick_convert_exec('-version', $output, $errors);
-    $form_element['version'] = array(
+    $element['version'] = array(
       '#type' => 'item',
       '#title' => t('Version information'),
-      '#markup' => '<pre>'. check_plain(trim($output)) .'</pre>',
+      '#markup' => '<pre>' . check_plain(trim($output)) . '</pre>',
       '#description' => t('ImageMagick was found and returns this version information.'),
     );
   }
-  return $form_element;
-}
-
-function imagemagick_validate_path($element, &$form_state) {
-  if ($form_state['values']['image_toolkit'] == 'imagemagick' && ($errors = _imagemagick_check_path($element['#value']))) {
-    form_error($element, implode('<br />', $errors));
-    //form_set_value($element['#parents'][0], variable_get('imagemagick_convert', '/usr/bin/convert'));
-  }
+  return $element;
 }
 
 /**
@@ -132,13 +140,14 @@ function _imagemagick_check_path($path) 
  *   The new width of the resized image, in pixels.
  * @param $height
  *   The new height of the resized image, in pixels.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
  * @see image_resize()
  */
 function image_imagemagick_resize(stdClass $image, $width, $height) {
-  $image->ops[] = '-resize '. (int) $width .'x'. (int) $height .'!';
+  $image->ops[] = '-resize ' . (int) $width . 'x' . (int) $height . '!';
   $image->info['width'] = $width;
   $image->info['height'] = $height;
   return TRUE;
@@ -158,6 +167,7 @@ function image_imagemagick_resize(stdCla
  *   0xff00ff for magenta, and 0xffffff for white. For images that support
  *   transparency, this will default to transparent. Otherwise it will
  *   be white.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
@@ -165,16 +175,16 @@ function image_imagemagick_resize(stdCla
  */
 function image_imagemagick_rotate(stdClass $image, $degrees, $background = NULL) {
   if (is_null($background)) {
-    $image->ops[] = ' -rotate '. (float) $degrees;
+    $image->ops[] = ' -rotate ' . (float) $degrees;
   }
   else {
     if (is_int($bgcolor)) {
-      $bgcolor = '#'. str_pad(dechex($bgcolor), 6, 0);
+      $bgcolor = '#' . str_pad(dechex($bgcolor), 6, 0);
     }
     else {
       $bgcolor = str_replace('0x', '#', $bgcolor);
     }
-    $image->ops[] = '-background '. escapeshellarg($bgcolor) .' -rotate '. (float) $degrees;
+    $image->ops[] = '-background ' . escapeshellarg($bgcolor) . ' -rotate ' . (float) $degrees;
   }
   return TRUE;
 }
@@ -193,13 +203,14 @@ function image_imagemagick_rotate(stdCla
  *   The width of the cropped area, in pixels.
  * @param $height
  *   The height of the cropped area, in pixels.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
  * @see image_crop()
  */
 function image_imagemagick_crop(stdClass $image, $x, $y, $width, $height) {
-  $image->ops[]  = '-crop '. (int) $width .'x'. (int) $height .'+'. (int) $x .'+'. (int) $y .'!';
+  $image->ops[] = '-crop ' . (int) $width . 'x' . (int) $height . '+' . (int) $x . '+' . (int) $y . '!';
   $image->info['width'] = $width;
   $image->info['height'] = $height;
   return TRUE;
@@ -210,6 +221,7 @@ function image_imagemagick_crop(stdClass
  *
  * @param $image
  *   An image object. The $image->resource value will be modified by this call.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
@@ -220,9 +232,20 @@ function image_imagemagick_desaturate(st
   return TRUE;
 }
 
+/**
+ * Sharpens an image.
+ *
+ * @param $image
+ *   An image object. The $image->resource value will be modified by this call.
+ *
+ * @return
+ *   TRUE or FALSE, based on success.
+ *
+ * @see image_sharpen()
+ */
 function image_imagemagick_sharpen(stdClass $image, $radius, $sigma, $amount, $threshold) {
-  $unsharp_arg = $radius .'x'. $sigma .'+'. $amount/100 .'+'. $threshold;
-  $image->ops[] = '-unsharp '. $unsharp_arg;
+  $unsharp_arg = $radius . 'x' . $sigma . '+' . $amount / 100 . '+' . $threshold;
+  $image->ops[] = '-unsharp ' . $unsharp_arg;
   return TRUE;
 }
 
@@ -231,6 +254,7 @@ function image_imagemagick_sharpen(stdCl
  *
  * @param $image
  *   An image object. The $image->resource value will populated by this call.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
@@ -248,6 +272,7 @@ function image_imagemagick_load(stdClass
  *   An image object.
  * @param $destination
  *   A string file URI or path where the image should be saved.
+ *
  * @return
  *   TRUE or FALSE, based on success.
  *
@@ -261,11 +286,11 @@ function image_imagemagick_save(stdClass
  * Calls the convert executable with the specified filter.
  */
 function _imagemagick_convert($source, $dest, $args) {
-  $args['quality'] = '-quality '. escapeshellarg(variable_get('imagemagick_quality', 75));
+  $args['quality'] = '-quality ' . escapeshellarg(variable_get('imagemagick_quality', 75));
   // To make use of ImageMagick 6's parenthetical command grouping we need to make
   // the $source image the first parameter and $dest the last.
   // See http://www.imagemagick.org/Usage/basics/#cmdline for more info.
-  $command = escapeshellarg($source) .' '. implode(' ', $args) .' '. escapeshellarg($dest);
+  $command = escapeshellarg($source) . ' ' . implode(' ', $args) . ' ' . escapeshellarg($dest);
 
   if (0 != _imagemagick_convert_exec($command, $output, $errors)) {
     return FALSE;
@@ -289,15 +314,18 @@ function _imagemagick_convert_exec($comm
     // 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'. escapeshellarg($drupal_path) .' /B '. escapeshellarg($convert_path);
+    $convert_path = 'start "window title" /D' . escapeshellarg($drupal_path) . ' /B ' . escapeshellarg($convert_path);
   }
 
   $descriptors = array(
-    0 => array('pipe', 'r'), // stdin
-    1 => array('pipe', 'w'), // stdout
-    2 => array('pipe', 'w')  // stderr
+    // stdin
+    0 => array('pipe', 'r'),
+    // stdout
+    1 => array('pipe', 'w'),
+    // stderr
+    2 => array('pipe', 'w'),
   );
-  if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes, $drupal_path)) {
+  if ($h = proc_open($convert_path . ' ' . $command_args, $descriptors, $pipes, $drupal_path)) {
     $output = '';
     while (!feof($pipes[1])) {
       $output .= fgets($pipes[1]);
@@ -315,7 +343,7 @@ function _imagemagick_convert_exec($comm
 
     // Display debugging information to authorized users.
     if (variable_get('imagemagick_debugging', FALSE) && user_access('administer site configuration')) {
-      drupal_set_message(t('ImageMagick command: @command', array('@command' => $convert_path .' '. $command_args)));
+      drupal_set_message(t('ImageMagick command: @command', array('@command' => $convert_path . ' ' . $command_args)));
       drupal_set_message(t('ImageMagick output: @output', array('@output' => $output)));
     }
 
@@ -342,3 +370,4 @@ function _imagemagick_convert_exec($comm
 /**
  * @} End of "ingroup image".
  */
+
