Index: imageapi_imagemagick.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/imageapi/imageapi_imagemagick.module,v retrieving revision 1.25 diff -u -p -r1.25 imageapi_imagemagick.module --- imageapi_imagemagick.module 17 Oct 2010 18:03:13 -0000 1.25 +++ imageapi_imagemagick.module 11 Dec 2010 16:32:38 -0000 @@ -35,7 +35,7 @@ function image_imageapi_imagemagick_sett $form['imageapi_imagemagick_binary'] = array( '#type' => 'fieldset', - '#title' => t('ImageMagick Binary'), + '#title' => t('ImageMagick Binaries'), '#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.'), ); @@ -46,6 +46,16 @@ function image_imageapi_imagemagick_sett '#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'), '#element_validate' => array('imageapi_imagemagick_validate_path'), + '#weight' => 1, + ); + $form['imageapi_imagemagick_binary']['imageapi_imagemagick_identify'] = array( + '#type' => 'textfield', + '#title' => t('Path to the "identify" binary'), + '#default_value' => variable_get('imageapi_imagemagick_identify', '/usr/bin/identify'), + '#required' => TRUE, + '#description' => t('Specify the complete path to the ImageMagic identify binary. For example: /usr/bin/identify or C:\Program Files\ImageMagick-6.3.4-Q16\identify.exe'), + '#element_validate' => array('imageapi_imagemagick_validate_path'), + '#weight' => 3, ); $form['imageapi_imagemagick_binary']['imageapi_imagemagick_debugging'] = array( '#type' => 'checkbox', @@ -54,7 +64,8 @@ function image_imageapi_imagemagick_sett '#description' => t('Checking this option will display the ImageMagick commands and output to users with the administer site configuration permission.'), '#weight' => 2, ); - $form['imageapi_imagemagick_binary']['version'] = array('#weight' => -1); + $form['imageapi_imagemagick_binary']['version_convert'] = array('#weight' => 2); + $form['imageapi_imagemagick_binary']['version_identify'] = array('#weight' => 4); $form['imageapi_imagemagick_binary']['#after_build'] = array('_imageapi_imagemagick_build_version'); return $form; @@ -65,27 +76,46 @@ function _imageapi_imagemagick_build_ver if ($path_errors = _imageapi_imagemagick_check_path($form_state['values']['imageapi_imagemagick_convert'])) { // check here is primarily for pre-existing bad settings... // the #validate should prevent users from adding bad paths. - $form_element['imageapi_imagemagick_binary'] = array( + $form_element['imageapi_imagemagick_binary']['version_convert'] = array( '#markup' => '

'. implode('
', $path_errors) .'

', ); } else { _imageapi_imagemagick_convert_exec('-version', $output, $errors); - $form_element['imageapi_imagemagick_binary']['version'] = array( - '#title' => t('Version information'), + $form_element['imageapi_imagemagick_binary']['version_convert'] = array( + '#title' => t('Version information for convert'), '#markup' => '
'. check_plain(trim($output)) .'
', '#description' => t('The ImageMagick convert binary was located and return this version information.'), ); } - $form_element['imageapi_imagemagick_binary']['version']['#type'] = 'item'; - $form_element['imageapi_imagemagick_binary']['version']['#weight'] = -1; + $form_element['imageapi_imagemagick_binary']['version_convert']['#type'] = 'item'; + $form_element['imageapi_imagemagick_binary']['version_convert']['#weight'] = 2; + + // make sure path is set and valid before running after build. + if ($path_errors = _imageapi_imagemagick_check_path($form_state['values']['imageapi_imagemagick_identify'])) { + // check here is primarily for pre-existing bad settings... + // the #validate should prevent users from adding bad paths. + $form_element['imageapi_imagemagick_binary']['version_identify'] = array( + '#markup' => '

'. implode('
', $path_errors) .'

', + ); + } + else { + _imageapi_imagemagick_identify_exec('-version', $output, $errors); + $form_element['imageapi_imagemagick_binary']['version_identify'] = array( + '#title' => t('Version information for identify'), + '#markup' => '
'. check_plain(trim($output)) .'
', + '#description' => t('The ImageMagick identify binary was located and return this version information.'), + ); + } + $form_element['imageapi_imagemagick_binary']['version_identify']['#type'] = 'item'; + $form_element['imageapi_imagemagick_binary']['version_identify']['#weight'] = 4; + return $form_element; } function imageapi_imagemagick_validate_path($element) { - if ($element['#post'] == 'imageapi_imagemagick' && $errors = _imageapi_imagemagick_check_path($element['#value'])) { + if ($errors = _imageapi_imagemagick_check_path($element['#value'])) { form_set_error($element['#parents'][0], implode('
', $errors)); - //form_set_value($element['#parents'][0], variable_get('imageapi_imagemagick_convert', '/usr/bin/convert')); return FALSE; } return TRUE; @@ -97,13 +127,31 @@ function imageapi_imagemagick_quality_el } } + +function image_imageapi_imagemagick_get_info(stdClass $image) { + $details = FALSE; + if (0 == _imageapi_imagemagick_identify_exec('-format "%w|%h|%C" '.drupal_realpath($image->source), $output, $errors)) { + $data = explode('|', $output); + $extensions = array('GIF' => 'gif', 'JPEG' => 'jpg', 'PNG' => 'png'); + $mimetypes = array('GIF' => 'image/gif', 'JPEG' => 'image/jpeg', 'PNG' => 'image/png'); + + $details = array( + 'width' => $data[0], + 'height' => $data[1], + 'extension' => isset($extensions[$data[2]]) ? $extensions[$data[2]] : '', + 'mime_type' => isset($mimetypes[$data[2]]) ? $mimetypes[$data[2]] : '', + ); + } + return $details; +} + function image_imageapi_imagemagick_load(stdClass $image) { $image->ops = array(); return $image; } function image_imageapi_imagemagick_save(stdClass $image, $destination) { - return _imageapi_imagemagick_convert($image->source, $destination, $image->ops); + return _imageapi_imagemagick_convert(drupal_realpath($image->source), drupal_realpath($destination), $image->ops); } function image_imageapi_imagemagick_crop(stdClass $image, $x, $y, $width, $height) { @@ -177,9 +225,8 @@ function _imageapi_imagemagick_check_pat return $errors; } -function _imageapi_imagemagick_convert_exec($command_args, &$output, &$errors) { - $convert_path = variable_get('imageapi_imagemagick_convert', '/usr/bin/convert'); - if ($errors = _imageapi_imagemagick_check_path($convert_path)) { +function _imageapi_imagemagick_exec($path, $command_args, &$output, &$errors) { + if ($errors = _imageapi_imagemagick_check_path($path)) { watchdog('imageapi imagemagick', '!errors', array('!errors' => implode('
', $errors)), WATCHDOG_ERROR); return FALSE; } @@ -193,7 +240,7 @@ function _imageapi_imagemagick_convert_e // 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($drupal_path) .' /B '. escapeshellarg($convert_path); + $path = 'start "window title" /D'. escapeshellarg($drupal_path) .' /B '. escapeshellarg($path); } $descriptors = array( @@ -201,7 +248,7 @@ function _imageapi_imagemagick_convert_e 1 => array('pipe', 'w'), // stdout 2 => array('pipe', 'w') // stderr ); - if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes, $drupal_path)) { + if ($h = proc_open($path .' '. $command_args, $descriptors, $pipes, $drupal_path)) { $output = ''; while (!feof($pipes[1])) { $output .= fgets($pipes[1]); @@ -219,7 +266,7 @@ function _imageapi_imagemagick_convert_e // Display debugging information to authorized users. if (variable_get('imageapi_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' => $path .' '. $command_args))); drupal_set_message(t('ImageMagick output: @output', array('@output' => $output))); } @@ -242,3 +289,14 @@ function _imageapi_imagemagick_convert_e } return FALSE; } + +function _imageapi_imagemagick_convert_exec($command_args, &$output, &$errors) { + $path = variable_get('imageapi_imagemagick_convert', '/usr/bin/convert'); + return _imageapi_imagemagick_exec($path, $command_args, $output, $errors); +} + +function _imageapi_imagemagick_identify_exec($command_args, &$output, &$errors) { + $path = variable_get('imageapi_imagemagick_identify', '/usr/bin/identify'); + return _imageapi_imagemagick_exec($path, $command_args, $output, $errors); +} +