diff --git a/imagemagick.api.php b/imagemagick.api.php index b05fafe..12cd907 100644 --- a/imagemagick.api.php +++ b/imagemagick.api.php @@ -2,6 +2,68 @@ /** * @file - * API documentation for ImageMagick module. + * API documentation for the ImageMagick module. */ +/** + * @addtogroup hooks + * @{ + */ + +/** + * Alter an image as it is loaded by the ImageMagick toolkit. + * + * @param $image + * An image object. + * + * @see image_load() + * @see image_imagemagick_load() + */ +function hook_imagemagick_load_alter(stdClass $image) { +} + +/** + * Alter an image before it is saved by the ImageMagick toolkit. + * + * @param $image + * An image object. + * @param $context + * An associative array of information about the image being saved: + * - destination: The file URI where $image will be saved to. + * + * @see image_save() + * @see image_imagemagick_save() + */ +function hook_imagemagick_save_alter(stdClass $image, $context = array()) { +} + +/** + * Alter the arguments to the ImageMagick 'convert' command-line program. + * + * @param $args + * An array of arguments to the ImageMagick 'convert' command-line program. + * @param $context + * An associative array of information about the image being altered: + * - source: The filesystem path of the original image. + * - source_original: The original file URI of the image. + * - destination: The filesystem path for the derivative image. + * - destination_original: The original file URI for the derivative image. + * - destination_format: The target image format for the derivative image. + * Defaults to an empty string. + * + * ImageMagick automatically converts the target image to the format denoted by + * the file extension. However, since changing the file extension is not always + * an option (e.g., for derivative images of core Image module styles), you can + * specify an alternative derivative image format in + * $context['destination_format']. 'destination_format' is a string denoting a + * file extension. If not empty, it is passed to ImageMagick's convert binary in + * the syntax "[destination_format]:[destination]". + * + * @see http://www.imagemagick.org/script/command-line-processing.php#output + * @see http://www.imagemagick.org/Usage/files/#save + * + * @see _imagemagick_convert() + */ +function hook_imagemagick_arguments_alter($args, $context = array()) { +} + diff --git a/imagemagick.module b/imagemagick.module index c42f3b4..d9b6c4c 100644 --- a/imagemagick.module +++ b/imagemagick.module @@ -359,28 +359,47 @@ function image_imagemagick_get_info(stdClass $image) { /** * Calls the convert executable with the specified filter. */ -function _imagemagick_convert($source, $dest, $args) { +function _imagemagick_convert($source, $destination, $args) { + // Backup original paths for alter hook context. + $source_original = $source; + $destination_original = $destination; + $source = drupal_realpath($source); - $dest = drupal_realpath($dest); + $destination = drupal_realpath($destination); + $destination_format = ''; $args['quality'] = '-quality ' . escapeshellarg(variable_get('imagemagick_quality', 75)); // Allow other modules to alter the ImageMagick command line parameters. $context = array( - 'source' => $source, - 'destination' => $dest, + 'source' => &$source, + 'source_original' => $source_original, + 'destination' => &$destination, + 'destination_original' => $destination_original, + 'destination_format' => &$destination_format, ); drupal_alter('imagemagick_arguments', $args, $context); + // If the format of the derivative image was changed, concatenate the new + // image format and the destination path, delimited by a colon. + // @see http://www.imagemagick.org/script/command-line-processing.php#output + // @see hook_imagemagick_arguments_alter() + if ($destination_format !== '') { + $destination_format .= ':' . $destination; + } + else { + $destination_format = $destination; + } + // 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 - $command = escapeshellarg($source) . ' ' . implode(' ', $args) . ' ' . escapeshellarg($dest); + $command = escapeshellarg($source) . ' ' . implode(' ', $args) . ' ' . escapeshellarg($destination_format); if (_imagemagick_convert_exec($command, $output, $error) !== TRUE) { return FALSE; } - return file_exists($dest); + return file_exists($destination); } /**