3
* WordPress Imagick Image Editor
6
* @subpackage Image_Editor
10
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module
14
* @subpackage Image_Editor
15
* @uses WP_Image_Editor Extends class
17
class WP_Image_Editor_Imagick extends WP_Image_Editor {
19
protected $image = null; // Imagick Object
21
public function __destruct() {
22
if ( $this->image instanceof Imagick ) {
23
// we don't need the original in memory anymore
24
$this->image->clear();
25
$this->image->destroy();
30
* Checks to see if current environment supports Imagick.
32
* We require Imagick 2.2.0 or greater, based on whether the queryFormats()
33
* method can be called statically.
40
public static function test( $args = array() ) {
42
// First, test Imagick's extension and classes.
43
if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick' ) || ! class_exists( 'ImagickPixel' ) )
46
if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) )
49
$required_methods = array(
59
'setimagecompression',
60
'setimagecompressionquality',
69
// Now, test for deep requirements within Imagick.
70
if ( ! defined( 'imagick::COMPRESSION_JPEG' ) )
73
if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) )
80
* Checks to see if editor supports the mime-type specified.
85
* @param string $mime_type
88
public static function supports_mime_type( $mime_type ) {
89
$imagick_extension = strtoupper( self::get_extension( $mime_type ) );
91
if ( ! $imagick_extension )
94
// setIteratorIndex is optional unless mime is an animated format.
95
// Here, we just say no if you are missing it and aren't loading a jpeg.
96
if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' )
100
return ( (bool) @Imagick::queryFormats( $imagick_extension ) );
102
catch ( Exception $e ) {
108
* Loads image from $this->file into new Imagick Object.
113
* @return boolean|WP_Error True if loaded; WP_Error on failure.
115
public function load() {
116
if ( $this->image instanceof Imagick )
119
if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
120
return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
122
/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
123
// Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
124
@ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
127
$this->image = new Imagick( $this->file );
129
if( ! $this->image->valid() )
130
return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
132
// Select the first frame to handle animated images properly
133
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
134
$this->image->setIteratorIndex(0);
136
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
138
catch ( Exception $e ) {
139
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
142
$updated_size = $this->update_size();
143
if ( is_wp_error( $updated_size ) )
144
return $updated_size;
150
* Sets Image Compression quality on a 1-100% scale.
155
* @param int $quality Compression Quality. Range: [1,100]
156
* @return boolean|WP_Error True if set successfully; WP_Error on failure.
158
public function set_quality( $quality = null ) {
159
$quality_result = parent::set_quality( $quality );
160
if ( is_wp_error( $quality_result ) ) {
161
return $quality_result;
163
$quality = $this->get_quality();
167
if ( 'image/jpeg' == $this->mime_type ) {
168
$this->image->setImageCompressionQuality( $quality );
169
$this->image->setImageCompression( imagick::COMPRESSION_JPEG );
172
$this->image->setImageCompressionQuality( $quality );
175
catch ( Exception $e ) {
176
return new WP_Error( 'image_quality_error', $e->getMessage() );
183
* Sets or updates current image size.
191
protected function update_size( $width = null, $height = null ) {
193
if ( !$width || !$height ) {
195
$size = $this->image->getImageGeometry();
197
catch ( Exception $e ) {
198
return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file );
203
$width = $size['width'];
206
$height = $size['height'];
208
return parent::update_size( $width, $height );
212
* Resizes current image.
214
* At minimum, either a height or width must be provided.
215
* If one of the two is set to null, the resize will
216
* maintain aspect ratio according to the provided dimension.
221
* @param int|null $max_w Image width.
222
* @param int|null $max_h Image height.
223
* @param boolean $crop
224
* @return boolean|WP_Error
226
public function resize( $max_w, $max_h, $crop = false ) {
227
if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
230
$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
232
return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
233
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
236
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
241
* @TODO: Thumbnail is more efficient, given a newer version of Imagemagick.
242
* $this->image->thumbnailImage( $dst_w, $dst_h );
244
$this->image->scaleImage( $dst_w, $dst_h );
246
catch ( Exception $e ) {
247
return new WP_Error( 'image_resize_error', $e->getMessage() );
250
return $this->update_size( $dst_w, $dst_h );
254
* Resize multiple images from a single source.
259
* @param array $sizes {
260
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
262
* Either a height or width must be provided.
263
* If one of the two is set to null, the resize will
264
* maintain aspect ratio according to the provided dimension.
266
* @type array $size {
267
* @type int ['width'] Optional. Image width.
268
* @type int ['height'] Optional. Image height.
269
* @type bool $crop Optional. Whether to crop the image. Default false.
272
* @return array An array of resized images' metadata by size.
274
public function multi_resize( $sizes ) {
276
$orig_size = $this->size;
277
$orig_image = $this->image->getImage();
279
foreach ( $sizes as $size => $size_data ) {
280
if ( ! $this->image )
281
$this->image = $orig_image->getImage();
283
if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
287
if ( ! isset( $size_data['width'] ) ) {
288
$size_data['width'] = null;
290
if ( ! isset( $size_data['height'] ) ) {
291
$size_data['height'] = null;
294
if ( ! isset( $size_data['crop'] ) ) {
295
$size_data['crop'] = false;
298
$resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
300
if( ! is_wp_error( $resize_result ) ) {
301
$resized = $this->_save( $this->image );
303
$this->image->clear();
304
$this->image->destroy();
307
if ( ! is_wp_error( $resized ) && $resized ) {
308
unset( $resized['path'] );
309
$metadata[$size] = $resized;
313
$this->size = $orig_size;
316
$this->image = $orig_image;
327
* @param string|int $src The source file or Attachment ID.
328
* @param int $src_x The start x position to crop from.
329
* @param int $src_y The start y position to crop from.
330
* @param int $src_w The width to crop.
331
* @param int $src_h The height to crop.
332
* @param int $dst_w Optional. The destination width.
333
* @param int $dst_h Optional. The destination height.
334
* @param boolean $src_abs Optional. If the source crop points are absolute.
335
* @return boolean|WP_Error
337
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
344
$this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
345
$this->image->setImagePage( $src_w, $src_h, 0, 0);
347
if ( $dst_w || $dst_h ) {
348
// If destination width/height isn't specified, use same as
349
// width/height from source.
355
$this->image->scaleImage( $dst_w, $dst_h );
356
return $this->update_size();
359
catch ( Exception $e ) {
360
return new WP_Error( 'image_crop_error', $e->getMessage() );
362
return $this->update_size();
366
* Rotates current image counter-clockwise by $angle.
371
* @param float $angle
372
* @return boolean|WP_Error
374
public function rotate( $angle ) {
376
* $angle is 360-$angle because Imagick rotates clockwise
377
* (GD rotates counter-clockwise)
380
$this->image->rotateImage( new ImagickPixel('none'), 360-$angle );
382
// Since this changes the dimensions of the image, update the size.
383
$result = $this->update_size();
384
if ( is_wp_error( $result ) )
387
$this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 );
389
catch ( Exception $e ) {
390
return new WP_Error( 'image_rotate_error', $e->getMessage() );
396
* Flips current image.
401
* @param boolean $horz Flip along Horizontal Axis
402
* @param boolean $vert Flip along Vertical Axis
403
* @returns boolean|WP_Error
405
public function flip( $horz, $vert ) {
408
$this->image->flipImage();
411
$this->image->flopImage();
413
catch ( Exception $e ) {
414
return new WP_Error( 'image_flip_error', $e->getMessage() );
420
* Saves current image to file.
425
* @param string $destfilename
426
* @param string $mime_type
427
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
429
public function save( $destfilename = null, $mime_type = null ) {
430
$saved = $this->_save( $this->image, $destfilename, $mime_type );
432
if ( ! is_wp_error( $saved ) ) {
433
$this->file = $saved['path'];
434
$this->mime_type = $saved['mime-type'];
437
$this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
439
catch ( Exception $e ) {
440
return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
447
protected function _save( $image, $filename = null, $mime_type = null ) {
448
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
451
$filename = $this->generate_filename( null, null, $extension );
454
// Store initial Format
455
$orig_format = $this->image->getImageFormat();
457
$this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
458
$this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
460
// Reset original Format
461
$this->image->setImageFormat( $orig_format );
463
catch ( Exception $e ) {
464
return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
467
// Set correct file permissions
468
$stat = stat( dirname( $filename ) );
469
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
470
@ chmod( $filename, $perms );
472
/** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
475
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
476
'width' => $this->size['width'],
477
'height' => $this->size['height'],
478
'mime-type' => $mime_type,
483
* Streams current image to browser.
488
* @param string $mime_type
489
* @return boolean|WP_Error
491
public function stream( $mime_type = null ) {
492
list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
495
// Temporarily change format for stream
496
$this->image->setImageFormat( strtoupper( $extension ) );
498
// Output stream of image content
499
header( "Content-Type: $mime_type" );
500
print $this->image->getImageBlob();
502
// Reset Image to original Format
503
$this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
505
catch ( Exception $e ) {
506
return new WP_Error( 'image_stream_error', $e->getMessage() );