/[drupal]/contributions/modules/image/image.imagemagick.inc
ViewVC logotype

Contents of /contributions/modules/image/image.imagemagick.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Fri Mar 6 01:40:09 2009 UTC (8 months, 3 weeks ago) by sun
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA6, DRUPAL-6--1-0-BETA3, DRUPAL-6--1-0-ALPHA5, DRUPAL-6--1-0-BETA2, DRUPAL-6--1-0-BETA1, HEAD
Changes since 1.23: +2 -2 lines
File MIME type: text/x-php
#243895 by Hetta: Fixed "selected file could not be copied" errors with ImageMagick on some platforms.
1 <?php
2 // $Id: image.imagemagick.inc,v 1.23 2008/12/27 06:58:41 drewish 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 * @return
82 * Array of modified arguments.
83 */
84 function _image_imagemagick_alter_invoke($op, $filepath, $args) {
85 foreach (module_implements('imagemagick_alter') as $module) {
86 $function = $module .'_imagemagick_alter';
87 $function($op, $filepath, $args);
88 }
89 return $args;
90 }
91
92 /**
93 * Resize an image to the given width and height.
94 */
95 function image_imagemagick_resize($source, $dest, $width, $height) {
96 $args = array('resize' => '-resize '. (int) $width .'x'. (int) $height .'!');
97 $args = _image_imagemagick_alter_invoke('resize', $source, $args);
98 return _image_imagemagick_convert($source, $dest, $args);
99 }
100
101 /**
102 * Rotate an image.
103 */
104 function image_imagemagick_rotate($source, $dest, $degrees, $bg_color = 0x000000) {
105 $args = array(
106 'rotate' => '-rotate '. (float) $degrees,
107 'background' => '-background #'. str_pad(dechex($bg_color), 6, 0),
108 );
109 $args = _image_imagemagick_alter_invoke('rotate', $source, $args);
110 return _image_imagemagick_convert($source, $dest, $args);
111 }
112
113 /**
114 * Crop an image to the specified dimensions.
115 */
116 function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
117 $args = array('crop' => '-crop '. (int) $width .'x'. (int) $height .'+'. (int) $x .'+'. (int) $y .'!');
118 $args = _image_imagemagick_alter_invoke('crop', $source, $args);
119 return _image_imagemagick_convert($source, $dest, $args);
120 }
121
122 /**
123 * Calls the convert executable with the specified filter.
124 */
125 function _image_imagemagick_convert($source, $dest, $args) {
126 $command = implode(' ', array(
127 preg_replace("/[^A-Za-z0-9\!\.\-\+\_\/\040]/", '', implode(' ', $args)),
128 escapeshellarg($source),
129 escapeshellarg($dest),
130 ));
131
132 if (0 != _image_imagemagick_convert_exec($command, $output, $errors)) {
133 return FALSE;
134 }
135 return file_exists($dest);
136 }
137
138 function _image_imagemagick_convert_exec($command_args, &$output, &$errors) {
139 $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert');
140 if (!_image_imagemagick_check_path($convert_path)) {
141 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');
142 return FALSE;
143 }
144
145 if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
146 // Use Window's start command to avoid the "black window" from showing up:
147 // http://us3.php.net/manual/en/function.exec.php#56599
148 // Use /D to run the command from PHP's current working directory so the
149 // file paths don't have to be absolute.
150 $convert_path = 'start "window title" /D'. escapeshellarg(getcwd()) .' /B '. escapeshellarg($convert_path);
151 }
152
153 $descriptors = array(
154 0 => array('pipe', 'r'), // stdin
155 1 => array('pipe', 'w'), // stdout
156 2 => array('pipe', 'w') // stderr
157 );
158 if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes)) {
159 $output = '';
160 while (!feof($pipes[1])) {
161 $output .= fgets($pipes[1]);
162 }
163
164 $errors = '';
165 while (!feof($pipes[2])) {
166 $errors .= fgets($pipes[2]);
167 }
168
169 // Display debugging information to authorized users.
170 if (variable_get('image_imagemagick_debugging', FALSE) && user_access('administer site configuration')) {
171 drupal_set_message(t('ImageMagick command: @command', array('@command' => $convert_path .' '. $command_args)));
172 drupal_set_message(t('ImageMagick output: @output', array('@output' => $output)));
173 }
174
175 if ($errors) {
176 drupal_set_message(t('ImageMagick reported an error: %error.', array('%error' => $errors)), 'error');
177 }
178
179 fclose($pipes[0]);
180 fclose($pipes[1]);
181 fclose($pipes[2]);
182 return proc_close($h);
183 }
184 return FALSE;
185 }
186

  ViewVC Help
Powered by ViewVC 1.1.2