diff --git a/imagemagick.api.php b/imagemagick.api.php index b05fafe..cc0f9f2 100644 --- a/imagemagick.api.php +++ b/imagemagick.api.php @@ -2,6 +2,52 @@ /** * @file - * API documentation for ImageMagick module. + * API documentation for the ImageMagick module. */ +/** + * Alter an image as it is loaded by the ImageMagick toolkit. + * + * @param $image + * An image object. + * + * @ingroup hooks + */ +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: A string file URI where the image will be saved. + * + * @ingroup hooks + */ +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: A string filesystem path of the original image. + * - source_original: A string file URI of the original image. + * - destination: A string filesystem path of the derivative image. + * - destination_original: A string file URI of the derivative image. + * - destination_format: A string format of the derivative image. + * + * @see http://www.imagemagick.org/script/convert.php + * @see http://www.imagemagick.org/script/command-line-processing.php#output + * @see http://www.imagemagick.org/Usage/files/#save + * + * @ingroup hooks + */ +function hook_imagemagick_arguments_alter($args, $context = array()) { +} diff --git a/imagemagick.module b/imagemagick.module index c42f3b4..81e1bd5 100644 --- a/imagemagick.module +++ b/imagemagick.module @@ -359,28 +359,41 @@ 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 is being changed, append the + // the destination path to the new image format. + if (is_string($destination_format) && $destination_format != '') { + $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 ? $destination_format : $destination); if (_imagemagick_convert_exec($command, $output, $error) !== TRUE) { return FALSE; } - return file_exists($dest); + return file_exists($destination); } /** @@ -492,4 +505,3 @@ function _imagemagick_convert_exec($command_args, &$output = NULL, &$error = NUL /** * @} End of "ingroup image". */ -