Index: image.imagemagick.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/Attic/image.imagemagick.inc,v
retrieving revision 1.3.2.9
diff -u -r1.3.2.9 image.imagemagick.inc
--- image.imagemagick.inc	10 Jul 2007 00:56:19 -0000	1.3.2.9
+++ image.imagemagick.inc	12 Jul 2007 16:24:55 -0000
@@ -8,11 +8,28 @@
   return array('name' => 'imagemagick', 'title' => 'ImageMagick Toolkit.');
 }
 
+function image_imagemagick_options() {
+  $defaults = array(
+    'strip' => 0,
+    'colorspace' => 0,
+    'quality' => '',
+    'unsharp' => array(
+      'enabled' => 0,
+      'radius' => 0,
+      'sigma' => 1.0,
+      'amount' => 1.0,
+      'threshold' => 0.05,
+    ), 
+    'debug_ouput' => 0,
+  );
+  return variable_get('image_imagemagick_options', $defaults);
+}
 /**
  * Validate and return toolkit specific settings
  */
 function image_imagemagick_settings() {
   $form['#after_build'] = array('_image_imagemagick_build_version');
+  $form['#validate'] = array('_image_imagemagick_settings_validate' => array());
   
   $form['imagemagick_binary'] = array(
     '#type' => 'fieldset',
@@ -28,22 +45,94 @@
     '#description' => t('Specify the complete path to the ImageMagic <kbd>convert</kbd> binary. For example: <kbd>/usr/bin/convert</kbd> or <kbd>C:\Program Files\ImageMagick-6.3.4-Q16\convert.exe</kbd>'),
   );
 
-  $form['imagemagick_options'] = array(
+  $options = image_imagemagick_options();
+  $form['image_imagemagick_options'] = array(
     '#type' => 'fieldset',
     '#title' => t('ImageMagick Advanced Options'),
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
+    '#tree' => TRUE,
     '#description' => t('These are advanced options for controling the ImageMagick. If you do not understand what these settings do you should not change them.'),
   );
-  $form['imagemagick_options']['image_imagemagick_option_strip'] = array(
+
+  $form['image_imagemagick_options']['strip'] = array(
     '#type' => 'checkbox',
     '#title' => t('Strip the image of any profiles or comments'),
-    '#default_value' => variable_get('image_imagemagick_option_strip', 0),
+    '#default_value' => $options['strip'],
     '#description' => t('Checking this option may reduce file sizes by removing any color profiles or comments embedded in the image. <a href="!link">More information on -strip</a>', array('!link' => url('http://www.imagemagick.org/script/command-line-options.php#strip'))),
   );
+
+  $form['image_imagemagick_options']['colorspace'] = array(
+    '#type' => 'select',
+    '#title' => t('Force colorspace'),
+    '#default_value' => $options['colorspace'],
+    '#options' => array(0 => '<None>', 'RGB' => 'RGB', 'Gray' => 'Gray'),
+    '#description' => t('Checking this option will convert all images to the specified colorspace. <a href="!link">More information on -colorspace</a>', array('!link' => url('http://www.imagemagick.org/script/command-line-options.php#colorspace'))),
+  );
+
+  $form['image_imagemagick_options']['quality'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Quality'),
+    '#description' => t('Define the image quality for JPEG and PNG files. If you leave this blank ImageMagick will estimate the quality from the file. The value ranges from 0 to 100. Higher values mean better image quality but bigger files. <a href="!link">More information on -quality</a>', array('!link' => url('http://www.imagemagick.org/script/command-line-options.php#quality'))),
+    '#size' => 10,
+    '#maxlength' => 3,
+    '#default_value' => $options['quality'],
+    '#field_suffix' => t('%'),
+  );
+
+  $form['image_imagemagick_options']['unsharp'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Unsharpen mask'),
+    '#description' => t('Checking this option will apply an unsharpen filter to each image. The image is convolved with a Gaussian operator of the given radius and standard deviation (sigma). <a href="!link">More information on -unsharp</a>', array('!link' => url('http://www.imagemagick.org/script/command-line-options.php#unsharp'))),
+  );
+  $form['image_imagemagick_options']['unsharp']['enabled'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enabled'),
+    '#default_value' => $options['unsharp']['enabled'],
+    '#description' => t('Checking this option will apply an unsharpen filter to each image. '),
+  );
+  $form['image_imagemagick_options']['unsharp']['radius'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Radius'),
+    '#default_value' => $options['unsharp']['radius'],
+    '#description' => t('The radius of the Gaussian, in pixels, not counting the center pixel (default 0). For reasonable results, radius should be larger than sigma. Use a radius of 0 to have the method select a suitable radius.'),
+  );
+  $form['image_imagemagick_options']['unsharp']['sigma'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Sigma'),
+    '#default_value' => $options['unsharp']['sigma'],
+    '#description' => t('The standard deviation of the Gaussian, in pixels (default 1.0).'),
+  );
+  $form['image_imagemagick_options']['unsharp']['amount'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Amount'),
+    '#default_value' => $options['unsharp']['amount'],
+    '#description' => t('The percentage of the difference between the original and the blur image that is added back into the original (default 1.0).'),
+  );
+  $form['image_imagemagick_options']['unsharp']['threshold'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Threshold'),
+    '#default_value' => $options['unsharp']['threshold'],
+    '#description' => t('The threshold, as a fraction of QuantumRange, needed to apply the difference amount (default 0.05).'),
+  );
+
+  $form['image_imagemagick_options']['debug_ouput'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Display commands and output'),
+    '#default_value' => $options['debug_ouput'],
+    '#description' => t('Checking this option will display the ImageMagick commands and output. This may be of use while debugging. It is best to leave this unchecked.'),
+  );
   return $form;
 }
 
+function _image_imagemagick_settings_validate($element) {
+  if (!empty($element['#post']['image_imagemagick_options']['quality'])) {
+    if (!is_numeric($element['#post']['image_imagemagick_options']['quality'])) {
+      form_set_error('image_imagemagick_options][quality', t('The quality must be a numeric value.'));
+    }
+  }
+}
+
 function _image_imagemagick_build_version($form, $form_element) {
   // Don't check the path when another toolkit is being selected.  
   if (!isset($form['#post']['image_toolkit']) || $form['#post']['image_toolkit'] == 'imagemagick') {
@@ -102,9 +191,17 @@
  * Calls the convert executable with the specified filter
  */
 function _image_imagemagick_convert($source, $dest, $filter) {
-  if (variable_get('image_imagemagick_option_strip', 0)) {
-    $filter .= ' -strip ';
+  $options = image_imagemagick_options();
+  $filter .= $options['strip']         ? ' -strip' : '';
+  $filter .= $options['colorspace']    ? ' -colorspace '. $options['colorspace'] : ''; 
+  $filter .= $options['quality'] != '' ? ' -quality '. (int) $options['quality'] : '';
+  if ($options['unsharp']['enabled']) {
+    $filter .= ' -unsharp '. (int) $options['unsharp']['radius'];
+    $filter .= $options['unsharp']['sigma'] != '' ? 'x'. (float) $options['unsharp']['sigma'] : '';
+    $filter .= $options['unsharp']['amount'] != '' ? '+'. (float) $options['unsharp']['amount'] : '';
+    $filter .= $options['unsharp']['threshold'] != '' ? '+'. (float) $options['unsharp']['threshold'] : '';
   }
+  $filter .= $options['debug_ouput']   ? ' -verbose' : '';
 
   $command = implode(' ', array(
     preg_replace("/[^A-Za-z0-9\!\.\-\+\040]/", '', $filter),
@@ -149,8 +246,11 @@
       $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)));
+    $options = image_imagemagick_options();
+    if (isset($options['debug_ouput']) && $options['debug_ouput']) {
+      drupal_set_message(t("ImageMagick command:<br /><code>@command</code><br />output:<br /><code>@output</code>", array('@command' => $convert_path .' '. $command_args, '@output' => $output)));
+    }
+
     if ($errors) {
       drupal_set_message(t("ImageMagick reported an error: %error", array('%error' => $errors)), 'error');
     }
