diff --git a/imagemagick.api.php b/imagemagick.api.php index b05fafe..c4edf10 100644 --- a/imagemagick.api.php +++ b/imagemagick.api.php @@ -2,6 +2,44 @@ /** * @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. + */ +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. + */ +function hook_imagemagick_save_alter(stdClass $image, $context = array()) { +} + +/** + * Alter the arguments to the ImageMagick 'convert' command-line program. + * + * @see http://www.imagemagick.org/script/convert.php + * + * @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_original: A string file URI of the original image. + * - destination_original: A string file URI of the derivative image. + * - source: A string filesystem path of the original image. + * - destination: A string filesystem path of the derivative image. + * - destination_format: A string format of the derivative image. + */ +function hook_imagemagick_arguments_alter($args, $context = array()) { +} \ No newline at end of file diff --git a/imagemagick.module b/imagemagick.module index c42f3b4..d642e8b 100644 --- a/imagemagick.module +++ b/imagemagick.module @@ -359,28 +359,37 @@ 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); // 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(_imagemagick_destination($destination, $context['destination_format'])); if (_imagemagick_convert_exec($command, $output, $error) !== TRUE) { return FALSE; } - return file_exists($dest); + return file_exists($destination); } /** @@ -490,6 +499,29 @@ function _imagemagick_convert_exec($command_args, &$output = NULL, &$error = NUL } /** - * @} End of "ingroup image". + * Format an image file destination path. + * + * Destination paths must be prepended with a format in order for 'convert' + * to write the destination file in a different format than the source file. + * + * @see http://www.imagemagick.org/script/command-line-processing.php#output + * @see http://www.imagemagick.org/Usage/files/#save + * + * @param $destination + * A string filesystem path. + * @param $format + * A string image format. + * + * @return + * A string filesystem path with the image format prepended. */ +function _imagemagick_destination($destination, $format = NULL) { + if (is_string($format) && $format != '') { + return $format . ':' . $destination; + } + return $destination; +} +/** + * @} End of "ingroup image". + */