|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* imagick PECL extension implementation for Image_Transform package |
|
7
|
|
|
* |
|
8
|
|
|
* PHP versions 4 and 5 |
|
9
|
|
|
* |
|
10
|
|
|
* LICENSE: This source file is subject to version 3.0 of the PHP license |
|
11
|
|
|
* that is available through the world-wide-web at the following URI: |
|
12
|
|
|
* http://www.php.net/license/3_0.txt. If you did not receive a copy of |
|
13
|
|
|
* the PHP License and are unable to obtain it through the web, please |
|
14
|
|
|
* send a note to [email protected] so we can mail you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @category Image |
|
17
|
|
|
* @package Image_Transform |
|
18
|
|
|
* @subpackage Image_Transform_Driver_Imagick2 |
|
19
|
|
|
* @author Alan Knowles <[email protected]> |
|
20
|
|
|
* @author Peter Bowyer <[email protected]> |
|
21
|
|
|
* @author Philippe Jausions <[email protected]> |
|
22
|
|
|
* @copyright 2002-2005 The PHP Group |
|
23
|
|
|
* @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
24
|
|
|
* @version CVS: $Id: Imagick2.php 266907 2008-10-01 21:10:50Z dufuz $ |
|
25
|
|
|
* @link http://pear.php.net/package/Image_Transform |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
//require_once __DIR__ . '/Image/Transform.php'; |
|
29
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Image/Transform.php'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* imagick PECL extension implementation for Image_Transform package |
|
33
|
|
|
* |
|
34
|
|
|
* WARNING: For version < 2.0 of the extension. For version 2.0 and up use |
|
35
|
|
|
* Imagick3 driver instead |
|
36
|
|
|
* |
|
37
|
|
|
* @category Image |
|
38
|
|
|
* @package Image_Transform |
|
39
|
|
|
* @subpackage Image_Transform_Driver_Imagick2 |
|
40
|
|
|
* @author Alan Knowles <[email protected]> |
|
41
|
|
|
* @author Peter Bowyer <[email protected]> |
|
42
|
|
|
* @author Philippe Jausions <[email protected]> |
|
43
|
|
|
* @copyright 2002-2005 The PHP Group |
|
44
|
|
|
* @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
45
|
|
|
* @version Release: @package_version@ |
|
46
|
|
|
* @link http://pear.php.net/package/Image_Transform |
|
47
|
|
|
* @since PHP 4.0 |
|
48
|
|
|
*/ |
|
49
|
|
|
class Image_Transform_Driver_Imagick2 extends Image_Transform |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* Handler of the imagick image ressource |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
public $imageHandle = null; |
|
56
|
|
|
/** |
|
57
|
|
|
* Handler of the image ressource before |
|
58
|
|
|
* the last transformation |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
public $oldImage; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @see __construct() |
|
65
|
|
|
*/ |
|
66
|
|
|
public function Image_Transform_Driver_Imagick2() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->__construct(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// End Image_Transform_Driver_Imagick2 |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @see http://www.imagemagick.org/www/formats.html |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct() |
|
77
|
|
|
{ |
|
78
|
|
|
if (PEAR::loadExtension('imagick')) { |
|
79
|
|
|
require_once __DIR__ . '/Image/Transform/Driver/Imagick/ImageTypes.php'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->isError(PEAR::raiseError('Couldn\'t find the imagick extension.', IMAGE_TRANSFORM_ERROR_UNSUPPORTED)); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Loads an image |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $image filename |
|
89
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
90
|
|
|
* @access public |
|
91
|
|
|
*/ |
|
92
|
|
|
public function load($image) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!($this->imageHandle = imagick_readimage($image))) { |
|
|
|
|
|
|
95
|
|
|
$this->free(); |
|
96
|
|
|
|
|
97
|
|
|
return $this->raiseError('Couldn\'t load image.', IMAGE_TRANSFORM_ERROR_IO); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
if (imagick_iserror($this->imageHandle)) { |
|
|
|
|
|
|
100
|
|
|
return $this->raiseError('Couldn\'t load image.', IMAGE_TRANSFORM_ERROR_IO); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->image = $image; |
|
104
|
|
|
$result = $this->_get_image_details($image); |
|
105
|
|
|
if (PEAR::isError($result)) { |
|
106
|
|
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// End load |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Resize Action |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $new_x New width |
|
118
|
|
|
* @param int $new_y New height |
|
119
|
|
|
* @param mixed $options Optional parameters |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool|PEAR_Error TRUE or PEAR_Error object on error |
|
122
|
|
|
* @access protected |
|
123
|
|
|
*/ |
|
124
|
|
|
public function _resize($new_x, $new_y, $options = null) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
if (!imagick_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_UNKNOWN, 1, '!')) { |
|
|
|
|
|
|
127
|
|
|
return $this->raiseError('Couldn\'t resize image.', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->new_x = $new_x; |
|
131
|
|
|
$this->new_y = $new_y; |
|
132
|
|
|
|
|
133
|
|
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// End resize |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Rotates the current image |
|
140
|
|
|
* Note: color mask are currently not supported |
|
141
|
|
|
* |
|
142
|
|
|
* @param $angle |
|
143
|
|
|
* @param null $options |
|
|
|
|
|
|
144
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
145
|
|
|
* @access public |
|
146
|
|
|
*/ |
|
147
|
|
|
public function rotate($angle, $options = null) |
|
148
|
|
|
{ |
|
149
|
|
|
if (0 == ($angle % 360)) { |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
if (!imagick_rotate($this->imageHandle, $angle)) { |
|
|
|
|
|
|
153
|
|
|
return $this->raiseError('Cannot create a new imagick image for the rotation.', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->new_x = imagick_getwidth($this->imageHandle); |
|
|
|
|
|
|
157
|
|
|
$this->new_y = imagick_getheight($this->imageHandle); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// End rotate |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* addText |
|
166
|
|
|
* |
|
167
|
|
|
* @param mixed $params |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
170
|
|
|
* @access public |
|
171
|
|
|
*/ |
|
172
|
|
|
public function addText($params) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->oldImage = $this->imageHandle; |
|
175
|
|
|
$params = array_merge($this->_get_default_text_params(), $params); |
|
176
|
|
|
|
|
177
|
|
|
if (is_array($params['color'])) { |
|
178
|
|
|
$params['color'] = $this->colorarray2colorhex($params['color']); |
|
179
|
|
|
} else { |
|
180
|
|
|
$params['color'] = mb_strtolower($params['color']); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
static $cmds = [ |
|
184
|
|
|
'setfillcolor' => 'color', |
|
185
|
|
|
'setfontsize' => 'size', |
|
186
|
|
|
'setfontface' => 'font', |
|
187
|
|
|
]; |
|
188
|
|
|
imagick_begindraw($this->imageHandle); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
foreach ($cmds as $cmd => $v) { |
|
191
|
|
|
if (!call_user_func('imagick_' . $cmd, $this->imageHandle, $params[$v])) { |
|
192
|
|
|
return $this->raiseError("Problem with adding Text::{$v} = {$params[$v]}", IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
if (!imagick_drawannotation($this->imageHandle, $params['x'], $params['y'], $params['text'])) { |
|
|
|
|
|
|
196
|
|
|
return $this->raiseError('Problem with adding Text', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return true; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// End addText |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Saves the image to a file |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $filename the name of the file to write to |
|
208
|
|
|
* @param string $type |
|
209
|
|
|
* @param null $quality |
|
|
|
|
|
|
210
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
211
|
|
|
* @access public |
|
212
|
|
|
*/ |
|
213
|
|
|
public function save($filename, $type = '', $quality = null) |
|
214
|
|
|
{ |
|
215
|
|
|
$options = is_array($quality) ? $quality : []; |
|
|
|
|
|
|
216
|
|
|
if (is_numeric($quality)) { |
|
|
|
|
|
|
217
|
|
|
$options['quality'] = $quality; |
|
218
|
|
|
} |
|
219
|
|
|
$quality = $this->_getOption('quality', $options, 75); |
|
220
|
|
|
imagick_setcompressionquality($this->imageHandle, $quality); |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
if ($type && strcasecmp($type, $this->type) |
|
223
|
|
|
&& !imagick_convert($this->imageHandle, $type)) { |
|
|
|
|
|
|
224
|
|
|
return $this->raiseError('Couldn\'t save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
if (!imagick_write($this->imageHandle, $filename)) { |
|
|
|
|
|
|
228
|
|
|
return $this->raiseError('Couldn\'t save image to file.', IMAGE_TRANSFORM_ERROR_IO); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if (!$this->keep_settings_on_save) { |
|
232
|
|
|
$this->free(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return true; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
// End save |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Displays image without saving and lose changes |
|
242
|
|
|
* |
|
243
|
|
|
* This method adds the Content-type HTTP header |
|
244
|
|
|
* |
|
245
|
|
|
* @param mixed $type |
|
246
|
|
|
* @param null|mixed $quality |
|
247
|
|
|
* |
|
248
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
249
|
|
|
* @access public |
|
250
|
|
|
*/ |
|
251
|
|
|
public function display($type = '', $quality = null) |
|
252
|
|
|
{ |
|
253
|
|
|
$options = is_array($quality) ? $quality : []; |
|
254
|
|
|
if (is_numeric($quality)) { |
|
255
|
|
|
$options['quality'] = $quality; |
|
256
|
|
|
} |
|
257
|
|
|
$quality = $this->_getOption('quality', $options, 75); |
|
258
|
|
|
imagick_setcompressionquality($this->imageHandle, $quality); |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
if ($type && strcasecmp($type, $this->type) |
|
261
|
|
|
&& !imagick_convert($this->imageHandle, $type)) { |
|
|
|
|
|
|
262
|
|
|
return $this->raiseError('Couldn\'t save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
if (!($image = imagick_image2blob($this->imageHandle))) { |
|
|
|
|
|
|
265
|
|
|
return $this->raiseError('Couldn\'t display image.', IMAGE_TRANSFORM_ERROR_IO); |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
header('Content-type: ' . imagick_getmimetype($this->imageHandle)); |
|
|
|
|
|
|
268
|
|
|
echo $image; |
|
269
|
|
|
$this->free(); |
|
270
|
|
|
|
|
271
|
|
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Adjusts the image gamma |
|
276
|
|
|
* |
|
277
|
|
|
* @param float $outputgamma |
|
278
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
279
|
|
|
* @access public |
|
280
|
|
|
*/ |
|
281
|
|
|
public function gamma($outputgamma = 1.0) |
|
282
|
|
|
{ |
|
283
|
|
|
if (1.0 != $outputgamma) { |
|
284
|
|
|
imagick_gamma($this->imageHandle, $outputgamma); |
|
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return true; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Crops the image |
|
292
|
|
|
* |
|
293
|
|
|
* @param mixed $width |
|
294
|
|
|
* @param mixed $height |
|
295
|
|
|
* @param mixed $x |
|
296
|
|
|
* @param mixed $y |
|
297
|
|
|
* |
|
298
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
299
|
|
|
* @access public |
|
300
|
|
|
*/ |
|
301
|
|
|
public function crop($width, $height, $x = 0, $y = 0) |
|
302
|
|
|
{ |
|
303
|
|
|
// Sanity check |
|
304
|
|
|
if (!$this->intersects($width, $height, $x, $y)) { |
|
305
|
|
|
return PEAR::raiseError('Nothing to crop', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
|
306
|
|
|
} |
|
307
|
|
|
if (!imagick_crop($this->imageHandle, $x, $y, $width, $height)) { |
|
|
|
|
|
|
308
|
|
|
return $this->raiseError('Couldn\'t crop image.', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
// I think that setting img_x/y is wrong, but scaleByLength() & friends |
|
312
|
|
|
// mess up the aspect after a crop otherwise. |
|
313
|
|
|
$this->new_x = $width; |
|
314
|
|
|
$this->new_y = $height; |
|
315
|
|
|
|
|
316
|
|
|
return true; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Horizontal mirroring |
|
321
|
|
|
* |
|
322
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
323
|
|
|
* @access public |
|
324
|
|
|
*/ |
|
325
|
|
|
public function mirror() |
|
326
|
|
|
{ |
|
327
|
|
|
if (!imagick_flop($this->imageHandle)) { |
|
|
|
|
|
|
328
|
|
|
return $this->raiseError('Couldn\'t mirror the image.', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
return true; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Vertical mirroring |
|
336
|
|
|
* |
|
337
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
338
|
|
|
* @access public |
|
339
|
|
|
*/ |
|
340
|
|
|
public function flip() |
|
341
|
|
|
{ |
|
342
|
|
|
if (!imagick_flip($this->imageHandle)) { |
|
|
|
|
|
|
343
|
|
|
return $this->raiseError('Couldn\'t flip the image.', IMAGE_TRANSFORM_ERROR_FAILED); |
|
|
|
|
|
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
return true; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Destroy image handle |
|
351
|
|
|
* |
|
352
|
|
|
* @access public |
|
353
|
|
|
*/ |
|
354
|
|
|
public function free() |
|
355
|
|
|
{ |
|
356
|
|
|
if (is_resource($this->imageHandle)) { |
|
|
|
|
|
|
357
|
|
|
imagick_destroyhandle($this->imageHandle); |
|
|
|
|
|
|
358
|
|
|
} |
|
359
|
|
|
$this->imageHandle = null; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* RaiseError Method - shows imagick Raw errors. |
|
364
|
|
|
* |
|
365
|
|
|
* @param string $message message = prefixed message.. |
|
366
|
|
|
* @param int $code error code |
|
367
|
|
|
* @return PEAR error object |
|
368
|
|
|
* @access protected |
|
369
|
|
|
*/ |
|
370
|
|
|
public function raiseError($message, $code = 0) |
|
371
|
|
|
{ |
|
372
|
|
|
if (is_resource($this->imageHandle)) { |
|
|
|
|
|
|
373
|
|
|
$message .= "\nReason: " . imagick_failedreason($this->imageHandle) . "\nDescription: " . imagick_faileddescription($this->imageHandle); |
|
|
|
|
|
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
return PEAR::raiseError($message, $code); |
|
377
|
|
|
} |
|
378
|
|
|
} // End class Image_Transform_Driver_Imagick2 |
|
379
|
|
|
|