0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* WordPress Imagick Image Editor |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Image_Editor |
|
7 |
*/ |
|
8 |
|
|
9 |
/** |
|
10 |
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module |
|
11 |
* |
|
12 |
* @since 3.5.0 |
|
13 |
* @package WordPress |
|
14 |
* @subpackage Image_Editor |
|
15 |
* @uses WP_Image_Editor Extends class |
|
16 |
*/ |
|
17 |
class WP_Image_Editor_Imagick extends WP_Image_Editor { |
5
|
18 |
/** |
|
19 |
* @var Imagick |
|
20 |
*/ |
|
21 |
protected $image; // Imagick Object |
0
|
22 |
|
5
|
23 |
public function __destruct() { |
0
|
24 |
if ( $this->image instanceof Imagick ) { |
|
25 |
// we don't need the original in memory anymore |
|
26 |
$this->image->clear(); |
|
27 |
$this->image->destroy(); |
|
28 |
} |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* Checks to see if current environment supports Imagick. |
|
33 |
* |
|
34 |
* We require Imagick 2.2.0 or greater, based on whether the queryFormats() |
|
35 |
* method can be called statically. |
|
36 |
* |
|
37 |
* @since 3.5.0 |
|
38 |
* @access public |
|
39 |
* |
|
40 |
* @return boolean |
|
41 |
*/ |
|
42 |
public static function test( $args = array() ) { |
|
43 |
|
|
44 |
// First, test Imagick's extension and classes. |
|
45 |
if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick' ) || ! class_exists( 'ImagickPixel' ) ) |
|
46 |
return false; |
|
47 |
|
|
48 |
if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) |
|
49 |
return false; |
|
50 |
|
|
51 |
$required_methods = array( |
|
52 |
'clear', |
|
53 |
'destroy', |
|
54 |
'valid', |
|
55 |
'getimage', |
|
56 |
'writeimage', |
|
57 |
'getimageblob', |
|
58 |
'getimagegeometry', |
|
59 |
'getimageformat', |
|
60 |
'setimageformat', |
|
61 |
'setimagecompression', |
|
62 |
'setimagecompressionquality', |
|
63 |
'setimagepage', |
|
64 |
'scaleimage', |
|
65 |
'cropimage', |
|
66 |
'rotateimage', |
|
67 |
'flipimage', |
|
68 |
'flopimage', |
|
69 |
); |
|
70 |
|
|
71 |
// Now, test for deep requirements within Imagick. |
|
72 |
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) |
|
73 |
return false; |
|
74 |
|
|
75 |
if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) ) |
|
76 |
return false; |
|
77 |
|
|
78 |
return true; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Checks to see if editor supports the mime-type specified. |
|
83 |
* |
|
84 |
* @since 3.5.0 |
|
85 |
* @access public |
|
86 |
* |
|
87 |
* @param string $mime_type |
|
88 |
* @return boolean |
|
89 |
*/ |
|
90 |
public static function supports_mime_type( $mime_type ) { |
|
91 |
$imagick_extension = strtoupper( self::get_extension( $mime_type ) ); |
|
92 |
|
|
93 |
if ( ! $imagick_extension ) |
|
94 |
return false; |
|
95 |
|
|
96 |
// setIteratorIndex is optional unless mime is an animated format. |
|
97 |
// Here, we just say no if you are missing it and aren't loading a jpeg. |
|
98 |
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' ) |
|
99 |
return false; |
|
100 |
|
|
101 |
try { |
|
102 |
return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); |
|
103 |
} |
|
104 |
catch ( Exception $e ) { |
|
105 |
return false; |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* Loads image from $this->file into new Imagick Object. |
|
111 |
* |
|
112 |
* @since 3.5.0 |
|
113 |
* @access protected |
|
114 |
* |
|
115 |
* @return boolean|WP_Error True if loaded; WP_Error on failure. |
|
116 |
*/ |
|
117 |
public function load() { |
|
118 |
if ( $this->image instanceof Imagick ) |
|
119 |
return true; |
|
120 |
|
|
121 |
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) |
|
122 |
return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); |
|
123 |
|
5
|
124 |
/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */ |
0
|
125 |
// Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits |
|
126 |
@ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
|
127 |
|
|
128 |
try { |
|
129 |
$this->image = new Imagick( $this->file ); |
|
130 |
|
|
131 |
if( ! $this->image->valid() ) |
|
132 |
return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file); |
|
133 |
|
|
134 |
// Select the first frame to handle animated images properly |
|
135 |
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) |
|
136 |
$this->image->setIteratorIndex(0); |
|
137 |
|
|
138 |
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); |
|
139 |
} |
|
140 |
catch ( Exception $e ) { |
|
141 |
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); |
|
142 |
} |
|
143 |
|
|
144 |
$updated_size = $this->update_size(); |
5
|
145 |
if ( is_wp_error( $updated_size ) ) { |
|
146 |
return $updated_size; |
|
147 |
} |
0
|
148 |
|
|
149 |
return $this->set_quality(); |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* Sets Image Compression quality on a 1-100% scale. |
|
154 |
* |
|
155 |
* @since 3.5.0 |
|
156 |
* @access public |
|
157 |
* |
|
158 |
* @param int $quality Compression Quality. Range: [1,100] |
5
|
159 |
* @return boolean|WP_Error True if set successfully; WP_Error on failure. |
0
|
160 |
*/ |
|
161 |
public function set_quality( $quality = null ) { |
5
|
162 |
$quality_result = parent::set_quality( $quality ); |
|
163 |
if ( is_wp_error( $quality_result ) ) { |
|
164 |
return $quality_result; |
|
165 |
} else { |
|
166 |
$quality = $this->get_quality(); |
|
167 |
} |
0
|
168 |
|
|
169 |
try { |
5
|
170 |
if ( 'image/jpeg' == $this->mime_type ) { |
|
171 |
$this->image->setImageCompressionQuality( $quality ); |
0
|
172 |
$this->image->setImageCompression( imagick::COMPRESSION_JPEG ); |
|
173 |
} |
|
174 |
else { |
|
175 |
$this->image->setImageCompressionQuality( $quality ); |
|
176 |
} |
|
177 |
} |
|
178 |
catch ( Exception $e ) { |
|
179 |
return new WP_Error( 'image_quality_error', $e->getMessage() ); |
|
180 |
} |
|
181 |
|
5
|
182 |
return true; |
0
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Sets or updates current image size. |
|
187 |
* |
|
188 |
* @since 3.5.0 |
|
189 |
* @access protected |
|
190 |
* |
|
191 |
* @param int $width |
|
192 |
* @param int $height |
5
|
193 |
* |
|
194 |
* @return true|WP_Error |
0
|
195 |
*/ |
|
196 |
protected function update_size( $width = null, $height = null ) { |
|
197 |
$size = null; |
|
198 |
if ( !$width || !$height ) { |
|
199 |
try { |
|
200 |
$size = $this->image->getImageGeometry(); |
|
201 |
} |
|
202 |
catch ( Exception $e ) { |
|
203 |
return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file ); |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
if ( ! $width ) |
|
208 |
$width = $size['width']; |
|
209 |
|
|
210 |
if ( ! $height ) |
|
211 |
$height = $size['height']; |
|
212 |
|
|
213 |
return parent::update_size( $width, $height ); |
|
214 |
} |
|
215 |
|
|
216 |
/** |
|
217 |
* Resizes current image. |
|
218 |
* |
5
|
219 |
* At minimum, either a height or width must be provided. |
|
220 |
* If one of the two is set to null, the resize will |
|
221 |
* maintain aspect ratio according to the provided dimension. |
|
222 |
* |
0
|
223 |
* @since 3.5.0 |
|
224 |
* @access public |
|
225 |
* |
5
|
226 |
* @param int|null $max_w Image width. |
|
227 |
* @param int|null $max_h Image height. |
|
228 |
* @param boolean $crop |
0
|
229 |
* @return boolean|WP_Error |
|
230 |
*/ |
|
231 |
public function resize( $max_w, $max_h, $crop = false ) { |
|
232 |
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) |
|
233 |
return true; |
|
234 |
|
|
235 |
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|
236 |
if ( ! $dims ) |
|
237 |
return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); |
|
238 |
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
|
239 |
|
|
240 |
if ( $crop ) { |
|
241 |
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); |
|
242 |
} |
|
243 |
|
|
244 |
try { |
|
245 |
/** |
|
246 |
* @TODO: Thumbnail is more efficient, given a newer version of Imagemagick. |
|
247 |
* $this->image->thumbnailImage( $dst_w, $dst_h ); |
|
248 |
*/ |
|
249 |
$this->image->scaleImage( $dst_w, $dst_h ); |
|
250 |
} |
|
251 |
catch ( Exception $e ) { |
|
252 |
return new WP_Error( 'image_resize_error', $e->getMessage() ); |
|
253 |
} |
|
254 |
|
|
255 |
return $this->update_size( $dst_w, $dst_h ); |
|
256 |
} |
|
257 |
|
|
258 |
/** |
|
259 |
* Resize multiple images from a single source. |
|
260 |
* |
|
261 |
* @since 3.5.0 |
|
262 |
* @access public |
|
263 |
* |
|
264 |
* @param array $sizes { |
|
265 |
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
|
266 |
* |
5
|
267 |
* Either a height or width must be provided. |
|
268 |
* If one of the two is set to null, the resize will |
|
269 |
* maintain aspect ratio according to the provided dimension. |
|
270 |
* |
0
|
271 |
* @type array $size { |
5
|
272 |
* @type int ['width'] Optional. Image width. |
|
273 |
* @type int ['height'] Optional. Image height. |
0
|
274 |
* @type bool $crop Optional. Whether to crop the image. Default false. |
|
275 |
* } |
|
276 |
* } |
5
|
277 |
* @return array An array of resized images' metadata by size. |
0
|
278 |
*/ |
|
279 |
public function multi_resize( $sizes ) { |
|
280 |
$metadata = array(); |
|
281 |
$orig_size = $this->size; |
|
282 |
$orig_image = $this->image->getImage(); |
|
283 |
|
|
284 |
foreach ( $sizes as $size => $size_data ) { |
|
285 |
if ( ! $this->image ) |
|
286 |
$this->image = $orig_image->getImage(); |
|
287 |
|
5
|
288 |
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { |
0
|
289 |
continue; |
5
|
290 |
} |
0
|
291 |
|
5
|
292 |
if ( ! isset( $size_data['width'] ) ) { |
|
293 |
$size_data['width'] = null; |
|
294 |
} |
|
295 |
if ( ! isset( $size_data['height'] ) ) { |
|
296 |
$size_data['height'] = null; |
|
297 |
} |
|
298 |
|
|
299 |
if ( ! isset( $size_data['crop'] ) ) { |
0
|
300 |
$size_data['crop'] = false; |
5
|
301 |
} |
0
|
302 |
|
|
303 |
$resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
5
|
304 |
$duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); |
0
|
305 |
|
5
|
306 |
if ( ! is_wp_error( $resize_result ) && ! $duplicate ) { |
0
|
307 |
$resized = $this->_save( $this->image ); |
|
308 |
|
|
309 |
$this->image->clear(); |
|
310 |
$this->image->destroy(); |
|
311 |
$this->image = null; |
|
312 |
|
|
313 |
if ( ! is_wp_error( $resized ) && $resized ) { |
|
314 |
unset( $resized['path'] ); |
|
315 |
$metadata[$size] = $resized; |
|
316 |
} |
|
317 |
} |
|
318 |
|
|
319 |
$this->size = $orig_size; |
|
320 |
} |
|
321 |
|
|
322 |
$this->image = $orig_image; |
|
323 |
|
|
324 |
return $metadata; |
|
325 |
} |
|
326 |
|
|
327 |
/** |
|
328 |
* Crops Image. |
|
329 |
* |
|
330 |
* @since 3.5.0 |
|
331 |
* @access public |
|
332 |
* |
|
333 |
* @param int $src_x The start x position to crop from. |
|
334 |
* @param int $src_y The start y position to crop from. |
|
335 |
* @param int $src_w The width to crop. |
|
336 |
* @param int $src_h The height to crop. |
|
337 |
* @param int $dst_w Optional. The destination width. |
|
338 |
* @param int $dst_h Optional. The destination height. |
|
339 |
* @param boolean $src_abs Optional. If the source crop points are absolute. |
|
340 |
* @return boolean|WP_Error |
|
341 |
*/ |
|
342 |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|
343 |
if ( $src_abs ) { |
|
344 |
$src_w -= $src_x; |
|
345 |
$src_h -= $src_y; |
|
346 |
} |
|
347 |
|
|
348 |
try { |
|
349 |
$this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); |
|
350 |
$this->image->setImagePage( $src_w, $src_h, 0, 0); |
|
351 |
|
|
352 |
if ( $dst_w || $dst_h ) { |
|
353 |
// If destination width/height isn't specified, use same as |
|
354 |
// width/height from source. |
|
355 |
if ( ! $dst_w ) |
|
356 |
$dst_w = $src_w; |
|
357 |
if ( ! $dst_h ) |
|
358 |
$dst_h = $src_h; |
|
359 |
|
|
360 |
$this->image->scaleImage( $dst_w, $dst_h ); |
|
361 |
return $this->update_size(); |
|
362 |
} |
|
363 |
} |
|
364 |
catch ( Exception $e ) { |
|
365 |
return new WP_Error( 'image_crop_error', $e->getMessage() ); |
|
366 |
} |
|
367 |
return $this->update_size(); |
|
368 |
} |
|
369 |
|
|
370 |
/** |
|
371 |
* Rotates current image counter-clockwise by $angle. |
|
372 |
* |
|
373 |
* @since 3.5.0 |
|
374 |
* @access public |
|
375 |
* |
|
376 |
* @param float $angle |
|
377 |
* @return boolean|WP_Error |
|
378 |
*/ |
|
379 |
public function rotate( $angle ) { |
|
380 |
/** |
|
381 |
* $angle is 360-$angle because Imagick rotates clockwise |
|
382 |
* (GD rotates counter-clockwise) |
|
383 |
*/ |
|
384 |
try { |
|
385 |
$this->image->rotateImage( new ImagickPixel('none'), 360-$angle ); |
|
386 |
|
|
387 |
// Since this changes the dimensions of the image, update the size. |
|
388 |
$result = $this->update_size(); |
|
389 |
if ( is_wp_error( $result ) ) |
|
390 |
return $result; |
|
391 |
|
|
392 |
$this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 ); |
|
393 |
} |
|
394 |
catch ( Exception $e ) { |
|
395 |
return new WP_Error( 'image_rotate_error', $e->getMessage() ); |
|
396 |
} |
|
397 |
return true; |
|
398 |
} |
|
399 |
|
|
400 |
/** |
|
401 |
* Flips current image. |
|
402 |
* |
|
403 |
* @since 3.5.0 |
|
404 |
* @access public |
|
405 |
* |
|
406 |
* @param boolean $horz Flip along Horizontal Axis |
|
407 |
* @param boolean $vert Flip along Vertical Axis |
|
408 |
* @returns boolean|WP_Error |
|
409 |
*/ |
|
410 |
public function flip( $horz, $vert ) { |
|
411 |
try { |
|
412 |
if ( $horz ) |
|
413 |
$this->image->flipImage(); |
|
414 |
|
|
415 |
if ( $vert ) |
|
416 |
$this->image->flopImage(); |
|
417 |
} |
|
418 |
catch ( Exception $e ) { |
|
419 |
return new WP_Error( 'image_flip_error', $e->getMessage() ); |
|
420 |
} |
|
421 |
return true; |
|
422 |
} |
|
423 |
|
|
424 |
/** |
|
425 |
* Saves current image to file. |
|
426 |
* |
|
427 |
* @since 3.5.0 |
|
428 |
* @access public |
|
429 |
* |
|
430 |
* @param string $destfilename |
|
431 |
* @param string $mime_type |
|
432 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
|
433 |
*/ |
|
434 |
public function save( $destfilename = null, $mime_type = null ) { |
|
435 |
$saved = $this->_save( $this->image, $destfilename, $mime_type ); |
|
436 |
|
|
437 |
if ( ! is_wp_error( $saved ) ) { |
|
438 |
$this->file = $saved['path']; |
|
439 |
$this->mime_type = $saved['mime-type']; |
|
440 |
|
|
441 |
try { |
|
442 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) ); |
|
443 |
} |
|
444 |
catch ( Exception $e ) { |
|
445 |
return new WP_Error( 'image_save_error', $e->getMessage(), $this->file ); |
|
446 |
} |
|
447 |
} |
|
448 |
|
|
449 |
return $saved; |
|
450 |
} |
|
451 |
|
|
452 |
protected function _save( $image, $filename = null, $mime_type = null ) { |
|
453 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|
454 |
|
|
455 |
if ( ! $filename ) |
|
456 |
$filename = $this->generate_filename( null, null, $extension ); |
|
457 |
|
|
458 |
try { |
|
459 |
// Store initial Format |
|
460 |
$orig_format = $this->image->getImageFormat(); |
|
461 |
|
|
462 |
$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) ); |
|
463 |
$this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) ); |
|
464 |
|
|
465 |
// Reset original Format |
|
466 |
$this->image->setImageFormat( $orig_format ); |
|
467 |
} |
|
468 |
catch ( Exception $e ) { |
|
469 |
return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); |
|
470 |
} |
|
471 |
|
|
472 |
// Set correct file permissions |
|
473 |
$stat = stat( dirname( $filename ) ); |
|
474 |
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
|
475 |
@ chmod( $filename, $perms ); |
|
476 |
|
5
|
477 |
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
0
|
478 |
return array( |
5
|
479 |
'path' => $filename, |
|
480 |
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
|
481 |
'width' => $this->size['width'], |
|
482 |
'height' => $this->size['height'], |
0
|
483 |
'mime-type' => $mime_type, |
|
484 |
); |
|
485 |
} |
|
486 |
|
|
487 |
/** |
|
488 |
* Streams current image to browser. |
|
489 |
* |
|
490 |
* @since 3.5.0 |
|
491 |
* @access public |
|
492 |
* |
|
493 |
* @param string $mime_type |
|
494 |
* @return boolean|WP_Error |
|
495 |
*/ |
|
496 |
public function stream( $mime_type = null ) { |
|
497 |
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|
498 |
|
|
499 |
try { |
|
500 |
// Temporarily change format for stream |
|
501 |
$this->image->setImageFormat( strtoupper( $extension ) ); |
|
502 |
|
|
503 |
// Output stream of image content |
|
504 |
header( "Content-Type: $mime_type" ); |
|
505 |
print $this->image->getImageBlob(); |
|
506 |
|
|
507 |
// Reset Image to original Format |
|
508 |
$this->image->setImageFormat( $this->get_extension( $this->mime_type ) ); |
|
509 |
} |
|
510 |
catch ( Exception $e ) { |
|
511 |
return new WP_Error( 'image_stream_error', $e->getMessage() ); |
|
512 |
} |
|
513 |
|
|
514 |
return true; |
|
515 |
} |
|
516 |
} |