--- ../../../../../drupal-5.7-unpatched/sites/all/modules/image/image.imagemagick.inc 2008-04-22 16:42:00.000000000 -0400 +++ image.imagemagick.inc 2008-06-05 10:33:13.000000000 -0400 @@ -5,7 +5,7 @@ * Return information about the imagemagick toolkit. */ function image_imagemagick_info() { - return array('name' => 'imagemagick', 'title' => 'ImageMagick Toolkit.'); + return array('name' => 'imagemagick', 'title' => 'ImageMagick toolkit'); } /** @@ -18,14 +18,21 @@ function image_imagemagick_settings() { '#type' => 'fieldset', '#title' => t('ImageMagick Binary'), '#collapsible' => FALSE, - '#description' => t('ImageMagick is a standalone program used to manipulate images. To use it, it must be installed on your server and you need to know where it is located. If you are unsure of the exact path consult your ISP or server administrator.'), + '#description' => t('ImageMagick is a collection of standalone programs used to manipulate images. To use it, it must be installed on your server and you need to know where it is located. If you are unsure of the exact path consult your ISP or server administrator.'), ); $form['imagemagick_binary']['image_imagemagick_convert'] = array( '#type' => 'textfield', '#title' => t('Path to the "convert" binary'), '#default_value' => variable_get('image_imagemagick_convert', '/usr/bin/convert'), '#required' => TRUE, - '#description' => t('Specify the complete path to the ImageMagic convert binary. For example: /usr/bin/convert or C:\Program Files\ImageMagick-6.3.4-Q16\convert.exe'), + '#description' => t('Specify the complete path to the ImageMagick convert binary. For example: /usr/bin/convert or C:\Program Files\ImageMagick-6.3.4-Q16\convert.exe'), + ); + $form['imagemagick_binary']['image_imagemagick_identify'] = array( + '#type' => 'textfield', + '#title' => t('Path to the "identify" binary'), + '#default_value' => variable_get('image_imagemagick_identify', '/usr/bin/identify'), + '#required' => TRUE, + '#description' => t('Specify the complete path to the ImageMagick identify binary. This will usually be in the same place as the convert binary.'), ); $form['imagemagick_binary']['image_imagemagick_debugging'] = array( '#type' => 'checkbox', @@ -89,6 +96,60 @@ function _image_imagemagick_alter_invoke return $args; } +function _image_imagemagick_mime_type($file, $type) { + static $finfo = null; + if (function_exists('finfo_open')) { + if (is_null($finfo)) + $finfo = finfo_open(FILEINFO_MIME); + return finfo_file($finfo, $file); + } + elseif (function_exists('mime_content_type')) + return mime_content_type($file); + else { + switch ($type) { + case 'pdf': + return 'application/pdf'; + case 'ai': + case 'ps': + case 'eps': + return 'application/postscript'; + default: + return "image/$type"; + } + } +} + +/** + * Return info about the given image file. + */ +function image_imagemagick_get_info($file) { + static $cache = array(); + if (!isset($cache[$file])) { + _image_imagemagick_identify_exec("-format \"%w %h %m\" " . escapeshellarg($file), $output, $errors); + $results = explode(' ', $output); + if (count($results)) { + $type = strtolower($results[2]); + $details = array( + 'width' => $results[0], + 'height' => $results[1], + 'extension' => $type, + 'mime_type' => _image_imagemagick_mime_type($file, $type), + ); + $cache[$file] = $details; + } + else + $cache[$file] = FALSE; + } + return $cache[$file]; +} + +/** + * Convert an image from one type to another (based on file extension) + */ +function image_imagemagick_convert($source, $dest) { + return _image_imagemagick_convert($source, $dest, array()); +} + /** * Resize an image to the given width and height. */ @@ -137,17 +198,25 @@ function _image_imagemagick_convert($sou function _image_imagemagick_convert_exec($command_args, &$output, &$errors) { $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert'); - if (!_image_imagemagick_check_path($convert_path)) { + return _image_imagemagick_exec($convert_path, $command_args, $output, $errors); +} + +function _image_imagemagick_identify_exec($command_args, &$output, &$errors) { + $identify_path = variable_get('image_imagemagick_identify', '/usr/bin/identify'); + return _image_imagemagick_exec($identify_path, $command_args, $output, $errors); +} + +function _image_imagemagick_exec($command_path, $command_args, &$output, &$errors) { + if (!_image_imagemagick_check_path($command_path)) { drupal_set_message(t("ImageMagick could not be found. The admin will need to set the path on the image toolkit page.", array('@image-toolkit-settings' => url('admin/settings/image-toolkit'))), 'error'); return FALSE; } - if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) { // Use Window's start command to avoid the "black window" from showing up: // http://us3.php.net/manual/en/function.exec.php#56599 // Use /D to run the command from PHP's current working directory so the // file paths don't have to be absolute. - $convert_path = 'start "window title" /D'. escapeshellarg(getcwd()) .' /B '. escapeshellarg($convert_path); + $command_path = 'start "window title" /D'. escapeshellarg(getcwd()) .' /B '. escapeshellarg($command_path); } $descriptors = array( @@ -155,7 +224,7 @@ function _image_imagemagick_convert_exec 1 => array('pipe', 'w'), // stdout 2 => array('pipe', 'w') // stderr ); - if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes)) { + if ($h = proc_open($command_path .' '. $command_args, $descriptors, $pipes)) { $output = ''; while (!feof($pipes[1])) { $output .= fgets($pipes[1]); @@ -168,7 +237,7 @@ function _image_imagemagick_convert_exec // Display debugging information to authorized users. if (variable_get('image_imagemagick_debugging', FALSE) && user_access('administer site configuration')) { - drupal_set_message(t("ImageMagick command: @command", array('@command' => $convert_path .' '. $command_args))); + drupal_set_message(t("ImageMagick command: @command", array('@command' => $command_path .' '. $command_args))); drupal_set_message(t("ImageMagick output: @output", array('@output' => $output))); }