| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: image.imagemagick.inc,v 1.24.2.1 2010/08/03 17:43:00 sun Exp $ 3 4 /** 5 * Return information about the imagemagick toolkit. 6 */ 7 function image_imagemagick_info() { 8 return array('name' => 'imagemagick', 'title' => 'ImageMagick Toolkit.'); 9 } 10 11 /** 12 * Validate and return toolkit specific settings. 13 */ 14 function image_imagemagick_settings() { 15 $form['#after_build'] = array('_image_imagemagick_build_version'); 16 17 $form['imagemagick_binary'] = array( 18 '#type' => 'fieldset', 19 '#title' => t('ImageMagick binary'), 20 '#collapsible' => FALSE, 21 '#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.'), 22 ); 23 $form['imagemagick_binary']['image_imagemagick_convert'] = array( 24 '#type' => 'textfield', 25 '#title' => t('Path to the "convert" binary'), 26 '#default_value' => variable_get('image_imagemagick_convert', '/usr/bin/convert'), 27 '#required' => TRUE, 28 '#description' => t('Specify the complete path to the ImageMagic <kbd>convert</kbd> binary. For example: <kbd>/usr/bin/convert</kbd> or <kbd>C:\Program Files\ImageMagick-6.3.4-Q16\convert.exe</kbd>.'), 29 ); 30 $form['imagemagick_binary']['image_imagemagick_debugging'] = array( 31 '#type' => 'checkbox', 32 '#title' => t('Display debugging information'), 33 '#default_value' => variable_get('image_imagemagick_debugging', 0), 34 '#description' => t('Checking this option will display the ImageMagick commands and ouput to users with the <em>administer site configuration</em> permission.'), 35 ); 36 return $form; 37 } 38 39 function _image_imagemagick_build_version($form, $form_element) { 40 // Don't check the path when another toolkit is being selected. 41 if (!isset($form['#post']['image_toolkit']) || $form['#post']['image_toolkit'] == 'imagemagick') { 42 $valid = _image_imagemagick_check_path($form_element['values']['image_imagemagick_convert'], 'image_imagemagick_convert'); 43 if ($valid) { 44 _image_imagemagick_convert_exec('-version', $output, $errors); 45 $form['imagemagick_binary']['version'] = array( 46 '#type' => 'item', 47 '#value' => '<pre>' . check_plain(trim($output)) . '</pre>', 48 ); 49 } 50 } 51 return $form; 52 } 53 54 function _image_imagemagick_check_path($path, $attach_error_to = FALSE) { 55 if (file_exists($path)) { 56 return TRUE; 57 } 58 if ($attach_error_to) { 59 if ($open_basedir = ini_get('open_basedir')) { 60 form_set_error($attach_error_to, t("No file %file could be found. PHP's <a href='@open_basedir'>open_basedir</a> security restriction is set to %open_basedir, which may be interfering with the attempts to locate ImageMagick.", array('%file' => $path, '%open_basedir' => $open_basedir, '@open_basedir' => 'http://php.net/features.safe-mode#ini.open-basedir'))); 61 } 62 else { 63 form_set_error($attach_error_to, t('The specified ImageMagic path %file does not exist.', array('%file' => $path))); 64 } 65 } 66 return FALSE; 67 } 68 69 /** 70 * Invoke hook_imagemagick_alter(). 71 * 72 * Implementors of hook_imagemagick_alter() should accept three parameters: $op, 73 * $filepath and &$args (passed by reference), which are described below. 74 * 75 * @param $op 76 * String with the operation: 'resize', 'crop', 'rotate'. 77 * @param $filepath 78 * String containing the path to the image that is being processed. 79 * @param $args 80 * Array containing ImageMagick options. 81 * 82 * @return 83 * Array of modified arguments. 84 */ 85 function _image_imagemagick_alter_invoke($op, $filepath, $args) { 86 foreach (module_implements('imagemagick_alter') as $module) { 87 $function = $module . '_imagemagick_alter'; 88 $function($op, $filepath, $args); 89 } 90 return $args; 91 } 92 93 /** 94 * Resize an image to the given width and height. 95 */ 96 function image_imagemagick_resize($source, $dest, $width, $height) { 97 $args = array('resize' => '-resize ' . (int) $width . 'x' . (int) $height . '!'); 98 $args = _image_imagemagick_alter_invoke('resize', $source, $args); 99 return _image_imagemagick_convert($source, $dest, $args); 100 } 101 102 /** 103 * Rotate an image. 104 */ 105 function image_imagemagick_rotate($source, $dest, $degrees, $bg_color = 0x000000) { 106 $args = array( 107 'rotate' => '-rotate ' . (float) $degrees, 108 'background' => '-background #' . str_pad(dechex($bg_color), 6, 0), 109 ); 110 $args = _image_imagemagick_alter_invoke('rotate', $source, $args); 111 return _image_imagemagick_convert($source, $dest, $args); 112 } 113 114 /** 115 * Crop an image to the specified dimensions. 116 */ 117 function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) { 118 $args = array('crop' => '-crop ' . (int) $width . 'x' . (int) $height . '+' . (int) $x . '+' . (int) $y . '!'); 119 $args = _image_imagemagick_alter_invoke('crop', $source, $args); 120 return _image_imagemagick_convert($source, $dest, $args); 121 } 122 123 /** 124 * Calls the convert executable with the specified filter. 125 */ 126 function _image_imagemagick_convert($source, $dest, $args) { 127 $command = implode(' ', array( 128 preg_replace("/[^A-Za-z0-9\!\.\-\+\_\/\040]/", '', implode(' ', $args)), 129 escapeshellarg($source), 130 escapeshellarg($dest), 131 )); 132 133 if (0 != _image_imagemagick_convert_exec($command, $output, $errors)) { 134 return FALSE; 135 } 136 return file_exists($dest); 137 } 138 139 function _image_imagemagick_convert_exec($command_args, &$output, &$errors) { 140 $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert'); 141 if (!_image_imagemagick_check_path($convert_path)) { 142 drupal_set_message(t("ImageMagick could not be found. The admin will need to set the path on the <a href='@image-toolkit-settings'>image toolkit page</a>.", array('@image-toolkit-settings' => url('admin/settings/image-toolkit'))), 'error'); 143 return FALSE; 144 } 145 146 if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) { 147 // Use Window's start command to avoid the "black window" from showing up: 148 // http://us3.php.net/manual/en/function.exec.php#56599 149 // Use /D to run the command from PHP's current working directory so the 150 // file paths don't have to be absolute. 151 $convert_path = 'start "window title" /D' . escapeshellarg(getcwd()) . ' /B ' . escapeshellarg($convert_path); 152 } 153 154 $descriptors = array( 155 0 => array('pipe', 'r'), // stdin 156 1 => array('pipe', 'w'), // stdout 157 2 => array('pipe', 'w'), // stderr 158 ); 159 if ($h = proc_open($convert_path . ' ' . $command_args, $descriptors, $pipes)) { 160 $output = ''; 161 while (!feof($pipes[1])) { 162 $output .= fgets($pipes[1]); 163 } 164 165 $errors = ''; 166 while (!feof($pipes[2])) { 167 $errors .= fgets($pipes[2]); 168 } 169 170 // Display debugging information to authorized users. 171 if (variable_get('image_imagemagick_debugging', FALSE) && user_access('administer site configuration')) { 172 drupal_set_message(t('ImageMagick command: @command', array('@command' => $convert_path . ' ' . $command_args))); 173 drupal_set_message(t('ImageMagick output: @output', array('@output' => $output))); 174 } 175 176 if ($errors) { 177 drupal_set_message(t('ImageMagick reported an error: %error.', array('%error' => $errors)), 'error'); 178 } 179 180 fclose($pipes[0]); 181 fclose($pipes[1]); 182 fclose($pipes[2]); 183 return proc_close($h); 184 } 185 return FALSE; 186 } 187
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |