Index: image.imagemagick.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.imagemagick.inc,v
retrieving revision 1.15
diff -u -r1.15 image.imagemagick.inc
--- image.imagemagick.inc	19 Aug 2007 19:01:54 -0000	1.15
+++ image.imagemagick.inc	20 Aug 2007 10:12:29 -0000
@@ -64,17 +64,19 @@
 /**
  * Invoke hook_imagemagick_alter().
  *
- * Implementors of hook_imagemagick_alter() should accept two parameters: $op
- * and &$args (passed by reference), which are described below.
+ * Implementors of hook_imagemagick_alter() should accept three parameters: $op,
+ * $image and &$args (passed by reference), which are described below.
  *
  * @param $op
  *   String with the operation: 'resize', 'crop', 'rotate'.
+ * @param $image
+ *   String containing the path to the image that is being processed.
  * @param $args
  *   Array containing ImageMagick options.
  * @return
  *   Array of modified arguments.
  */
-function _image_imagemagick_alter_invoke($op, $args) {
+function _image_imagemagick_alter_invoke($op, $image, $args) {
   foreach (module_implements('imagemagick_alter') as $module) {
     $function = $module .'_imagemagick_alter'; 
     $function($op, $args);
@@ -87,7 +89,7 @@
  */
 function image_imagemagick_resize($source, $dest, $width, $height) {
   $args = array('resize' => '-resize '. $width .'x'. $height .'!');
-  $args = _image_imagemagick_alter_invoke('resize', $args);
+  $args = _image_imagemagick_alter_invoke('resize', $source, $args);
   return _image_imagemagick_convert($source, $dest, $args);
 }
 
@@ -99,7 +101,7 @@
     'rotate' => '-rotate '. (float) $degrees,
     'background' => '-background #'. str_pad(dechex($bg_color), 6, 0),
   );
-  $args = _image_imagemagick_alter_invoke('rotate', $args);
+  $args = _image_imagemagick_alter_invoke('rotate', $source, $args);
   return _image_imagemagick_convert($source, $dest, $args);
 }
 
@@ -108,7 +110,7 @@
  */
 function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
   $args = array('crop' => '-crop '. $width .'x'. $height .'+'. $x .'+'. $y);
-  $args = _image_imagemagick_alter_invoke('crop', $args);
+  $args = _image_imagemagick_alter_invoke('crop', $source, $args);
   return _image_imagemagick_convert($source, $dest, $args);
 }
 
Index: contrib/image_im_advanced/image_im_advanced.module
===================================================================
RCS file: contrib/image_im_advanced/image_im_advanced.module
diff -N contrib/image_im_advanced/image_im_advanced.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ contrib/image_im_advanced/image_im_advanced.module	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,180 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_form_alter.
+ * Add options to the Image toolkit settings form.
+ */
+function image_im_advanced_form_alter($form_id, &$form) {
+  if ($form_id == 'system_image_toolkit_settings') {
+    $form['imagemagick_advanced'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('ImageMagick Advanced Options'),
+      '#collapsible' => FALSE,
+      '#description' => t("These settings let you control some of ImageMagick's more advanced options."),
+    );
+    $form['imagemagick_advanced']['image_jpeg_quality'] = array(
+      '#type' => 'textfield',
+      '#title' => t('JPEG quality'),
+      '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
+      '#size' => 10,
+      '#maxlength' => 3,
+      '#default_value' => variable_get('image_jpeg_quality', 75),
+      '#field_suffix' => t('%'),
+    );
+    $form['imagemagick_advanced']['image_im_advanced_strip'] = array(
+      '#type' => 'textfield',
+      '#default_value' => variable_get('image_im_advanced_strip', '150x150'),
+      '#title' => t('Strip metadata from images at this size and below'),
+      '#description' => t('You may choose to strip all metadata, such as camera information and color profiles, from the processed images in order to reduce their file size. Please choose at what maximum size you want images to be stripped of their metadata. Example: "150x150". Enter "0x0" to disable this feature.'),
+    );
+    $form['imagemagick_advanced']['image_im_advanced_colorspace'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('image_im_advanced_colorspace', 1),
+      '#title' => t('Convert images to RGB'),
+      '#description' => t('Some images may be in other color spaces than RGB, such as CMYK. Those are not well suited for web use however, so it is recommended that all images are converted to RGB whenever they are processed.'),
+    );
+    $form['imagemagick_advanced']['image_im_advanced_density'] = array(
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('image_im_advanced_density', 1),
+      '#title' => t('Change image resolution to 72 ppi'),
+      '#description' => t('If checked, this option will set the print resolution of the image to 72 pixels per inch, which is suitable for web use. This does not affect the pixel size or quality of the image.'),
+    );
+    // Sharpening fieldset:
+    $form['imagemagick_advanced']['unsharp'] = array(
+      '#type' => 'fieldset',
+      '#collapsible' => true,
+      '#collapsed' => true,
+      '#title' => t('Sharpening filter'),
+      '#description' => t('The sharpness filter is used to regain some of the sharpness that is always lost when a digital photograph is scaled down. This is equivalent to the commonly used "Unsharp Mask" filter. It is important that these values are not set too high as it can easily make the images look artificial.'),
+    );
+    // Uncollapse fieldset if user has enabled sharpening:
+    if (variable_get('image_im_advanced_unsharp_amount', 0) != 0) {
+      $form['imagemagick_advanced']['unsharp']['#collapsed'] = false;
+    }
+    $form['imagemagick_advanced']['unsharp']['image_im_advanced_unsharp_amount'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Sharpness filter strength'),
+      '#default_value' => variable_get('image_im_advanced_unsharp_amount', 0),
+      '#size' => 4,
+      '#maxlength' => 3,
+      '#description' => t('Apply this percentage of sharpness when scaling. 90 is recommended, although values higher than 100 are also valid. Set to 0 to disable this feature.'),
+    );
+    $form['imagemagick_advanced']['unsharp']['image_im_advanced_unsharp_radius'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Sharpness filter radius'),
+      '#default_value' => variable_get('image_im_advanced_unsharp_radius', 0.9),
+      '#size' => 4,
+      '#maxlength' => 4,
+      '#description' => t('Use this pixel radius for the sharpness filter. 0.9 is recommended.'),
+    );
+    // Color profile fieldset:
+    $form['imagemagick_advanced']['profile'] = array(
+      '#type' => 'fieldset',
+      '#collapsible' => true,
+      '#collapsed' => true,
+      '#title' => t('Color profile'),
+      '#description' => t('Processed images may be converted to a color profile specified here. This is especially important when working with images that use a wide-gamut color profile such as ColorMatch or Adobe RGB, which is often the case with professional photography. sRGB (which may be downloaded from <a href="http://www.color.org/profiles.html">ICC</a>) is recommended since it is likely to look good on most displays.<br />Note that this conversion is still useful even if you choose to strip all metadata from your images (see above). This is because the conversion happens first and changes the actual image data before the profile is stripped.'),
+    );
+    // Uncollapse fieldset if user has specified a color profile:
+    if (variable_get('image_im_advanced_unsharp_profile', '') != '') {
+      $form['imagemagick_advanced']['profile']['#collapsed'] = false;
+    }
+    $form['imagemagick_advanced']['profile']['image_im_advanced_unsharp_profile'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Path to color profile'),
+      '#default_value' => variable_get('image_im_advanced_unsharp_profile', ''),
+      '#description' => t('The path to a color profile file that all scaled down images will be converted to. Leave empty to disable.'),
+    );
+    // Move the buttons to the bottom:
+    $form['buttons']['#weight'] = 10;
+    // Add validation function:
+    if (!is_array($form['#validate'])) {
+      $form['#validate'] = array();
+    }
+    $form['#validate'] += array('image_im_advanced_settings_validate' => array());
+    //$form['#validate'] = array('image_im_advanced_settings_validate' => array()) + $form['#validate'];
+  } 
+}
+
+
+/**
+ * Validate settings form.
+ */
+function image_im_advanced_settings_validate($form_id, $form_values, $form) {
+	if (!is_numeric($form_values['image_jpeg_quality']) || $form_values['image_jpeg_quality'] < 1 || $form_values['image_jpeg_quality'] > 100) {
+    form_set_error('image_jpeg_quality', t('The JPEG quality must be a positive number between 1 and 100.'));
+	}
+	if (!preg_match('/\d+x\d+/', $form_values['image_im_advanced_strip'])) {
+	  form_set_error('image_im_advanced_strip', t('The strip metadata threshold must be specified in the form <em>NxN</em>. Example: <em>150x150</em>.'));
+	}
+	if (!empty($form_values['image_im_advanced_unsharp_amount']) && (!is_numeric($form_values['image_im_advanced_unsharp_amount']) || $form_values['image_im_advanced_unsharp_amount'] < 0)) {
+	  form_set_error('image_im_advanced_unsharp_amount', t('The sharpness amount must be specified as a positive number.'));
+	}
+	if (!empty($form_values['image_im_advanced_unsharp_amount']) && (!is_numeric($form_values['image_im_advanced_unsharp_radius']) || $form_values['image_im_advanced_unsharp_radius'] < 0)) {
+	  form_set_error('image_im_advanced_unsharp_radius', t('The sharpness radius must be specified as a positive value.'));
+	}
+	  if (!is_readable($form_values['image_im_advanced_unsharp_profile'])) {
+    form_set_error('image_im_advanced_unsharp_profile', t('The ICC profile could not be read.'));
+  }
+}
+
+
+/**
+ * Implementation of hook_imagemagick_alter.
+ */
+function image_im_advanced_imagemagick_alter($op, $image, &$args) {
+  drupal_set_message('Before: <pre>' . var_export($args, true) . '</pre>');
+  $info = image_get_info($image);
+  switch ($op) {
+    case 'resize':
+      // Get destination image size:
+      $size = preg_replace('/[^\d]*(\d+x\d+)[^\d]*/', '$1', $args['resize']);
+      list($width, $height) = explode('x', $size);
+      // Add sharpening filter:
+      $unsharp_amount = (int) variable_get('image_im_advanced_unsharp_amount', 0);
+      $unsharp_radius = (float) variable_get('image_im_advanced_unsharp_radius', 0.9);
+      if ($unsharp_amount && $unsharp_radius) {
+        // 0.7 and 0.02 are reasonable values for Sigma and Threshold:
+        $args['unsharp'] = '-unsharp ' . $unsharp_radius . 'x0.7+' . round($unsharp_amount / 100, 2) . '+0.02';
+      }
+      // Convert to specified color profile:
+      $profile = variable_get('image_im_advanced_unsharp_profile', '');
+      if (!empty($profile) && file_exists($profile)) {
+        $args['profile'] = '-profile ' . $profile;
+      }
+      break;
+    case 'crop':
+      // Get destination image size:
+      $size = preg_replace('/[^\d]*(\d+x\d+)[^\d]*/', '$1', $args['crop']);
+      list($width, $height) = explode('x', $size);
+      break;
+    case 'rotate':
+      // Get image size:
+      $width = $info['width'];
+      $height = $info['height'];
+      break;
+  }
+  // Determine if the -strip parameter should be used:
+  list($strip_width, $strip_height) = explode('x', variable_get('image_im_advanced_strip', '150x150'));
+  if ((int) $width <= (int) $strip_width && (int) $height <= (int) $strip_height) {
+    $args['strip'] = '-strip';
+  }
+  if ($info['mime_type'] == 'image/jpeg') {
+    // Set JPEG quality:
+    $jpeg_quality = (int) variable_get('image_jpeg_quality', 75);
+    if (empty($args['quality']) && $jpeg_quality) {
+      $args['quality'] = "-quality $jpeg_quality";
+    }
+  }
+  // Convert to RGB:
+  if (variable_get('image_im_advanced_colorspace', 1)) {
+    $args['colorspace'] = '-colorspace RGB';
+  }
+  // Change image density (this doesn't affect the image dimensions/resolution):
+  if (variable_get('image_im_advanced_density', 1)) {
+    $args['density'] = '-density 72 -units PixelsPerInch';
+  }
+  drupal_set_message('After:<pre>' . var_export($args, true) . '</pre>');
+}
+
Index: contrib/image_im_advanced/image_im_advanced.info
===================================================================
RCS file: contrib/image_im_advanced/image_im_advanced.info
diff -N contrib/image_im_advanced/image_im_advanced.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ contrib/image_im_advanced/image_im_advanced.info	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,4 @@
+; $Id$
+name = ImageMagick Advanced Options
+description = Adds advanced options to the ImageMagick image toolkit.
+package = Image
