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 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 // Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
123 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
126 $this->image = new Imagick( $this->file );
128 if( ! $this->image->valid() )
129 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
131 // Select the first frame to handle animated images properly
132 if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
133 $this->image->setIteratorIndex(0);
135 $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
137 catch ( Exception $e ) {
138 return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
141 $updated_size = $this->update_size();
142 if ( is_wp_error( $updated_size ) )
143 return $updated_size;
145 return $this->set_quality();
149 * Sets Image Compression quality on a 1-100% scale.
154 * @param int $quality Compression Quality. Range: [1,100]
155 * @return boolean|WP_Error
157 public function set_quality( $quality = null ) {
159 $quality = $this->quality;
162 if( 'image/jpeg' == $this->mime_type ) {
163 $this->image->setImageCompressionQuality( apply_filters( 'jpeg_quality', $quality, 'image_resize' ) );
164 $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
167 $this->image->setImageCompressionQuality( $quality );
170 catch ( Exception $e ) {
171 return new WP_Error( 'image_quality_error', $e->getMessage() );
174 return parent::set_quality( $quality );
178 * Sets or updates current image size.
186 protected function update_size( $width = null, $height = null ) {
188 if ( !$width || !$height ) {
190 $size = $this->image->getImageGeometry();
192 catch ( Exception $e ) {
193 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file );
198 $width = $size['width'];
201 $height = $size['height'];
203 return parent::update_size( $width, $height );
207 * Resizes current image.
214 * @param boolean $crop
215 * @return boolean|WP_Error
217 public function resize( $max_w, $max_h, $crop = false ) {
218 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
221 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
223 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
224 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
227 return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
232 * @TODO: Thumbnail is more efficient, given a newer version of Imagemagick.
233 * $this->image->thumbnailImage( $dst_w, $dst_h );
235 $this->image->scaleImage( $dst_w, $dst_h );
237 catch ( Exception $e ) {
238 return new WP_Error( 'image_resize_error', $e->getMessage() );
241 return $this->update_size( $dst_w, $dst_h );
245 * Resize multiple images from a single source.
250 * @param array $sizes {
251 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
253 * @type array $size {
254 * @type int $width Image width.
255 * @type int $height Image height.
256 * @type bool $crop Optional. Whether to crop the image. Default false.
259 * @return array An array of resized images metadata by size.
261 public function multi_resize( $sizes ) {
263 $orig_size = $this->size;
264 $orig_image = $this->image->getImage();
266 foreach ( $sizes as $size => $size_data ) {
267 if ( ! $this->image )
268 $this->image = $orig_image->getImage();
270 if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )
273 if ( ! isset( $size_data['crop'] ) )
274 $size_data['crop'] = false;
276 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
278 if( ! is_wp_error( $resize_result ) ) {
279 $resized = $this->_save( $this->image );
281 $this->image->clear();
282 $this->image->destroy();
285 if ( ! is_wp_error( $resized ) && $resized ) {
286 unset( $resized['path'] );
287 $metadata[$size] = $resized;
291 $this->size = $orig_size;
294 $this->image = $orig_image;
305 * @param string|int $src The source file or Attachment ID.
306 * @param int $src_x The start x position to crop from.
307 * @param int $src_y The start y position to crop from.
308 * @param int $src_w The width to crop.
309 * @param int $src_h The height to crop.
310 * @param int $dst_w Optional. The destination width.
311 * @param int $dst_h Optional. The destination height.
312 * @param boolean $src_abs Optional. If the source crop points are absolute.
313 * @return boolean|WP_Error
315 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
322 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
323 $this->image->setImagePage( $src_w, $src_h, 0, 0);
325 if ( $dst_w || $dst_h ) {
326 // If destination width/height isn't specified, use same as
327 // width/height from source.
333 $this->image->scaleImage( $dst_w, $dst_h );
334 return $this->update_size();
337 catch ( Exception $e ) {
338 return new WP_Error( 'image_crop_error', $e->getMessage() );
340 return $this->update_size();
344 * Rotates current image counter-clockwise by $angle.
349 * @param float $angle
350 * @return boolean|WP_Error
352 public function rotate( $angle ) {
354 * $angle is 360-$angle because Imagick rotates clockwise
355 * (GD rotates counter-clockwise)
358 $this->image->rotateImage( new ImagickPixel('none'), 360-$angle );
360 // Since this changes the dimensions of the image, update the size.
361 $result = $this->update_size();
362 if ( is_wp_error( $result ) )
365 $this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 );
367 catch ( Exception $e ) {
368 return new WP_Error( 'image_rotate_error', $e->getMessage() );
374 * Flips current image.
379 * @param boolean $horz Flip along Horizontal Axis
380 * @param boolean $vert Flip along Vertical Axis
381 * @returns boolean|WP_Error
383 public function flip( $horz, $vert ) {
386 $this->image->flipImage();
389 $this->image->flopImage();
391 catch ( Exception $e ) {
392 return new WP_Error( 'image_flip_error', $e->getMessage() );
398 * Saves current image to file.
403 * @param string $destfilename
404 * @param string $mime_type
405 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
407 public function save( $destfilename = null, $mime_type = null ) {
408 $saved = $this->_save( $this->image, $destfilename, $mime_type );
410 if ( ! is_wp_error( $saved ) ) {
411 $this->file = $saved['path'];
412 $this->mime_type = $saved['mime-type'];
415 $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
417 catch ( Exception $e ) {
418 return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
425 protected function _save( $image, $filename = null, $mime_type = null ) {
426 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
429 $filename = $this->generate_filename( null, null, $extension );
432 // Store initial Format
433 $orig_format = $this->image->getImageFormat();
435 $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
436 $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
438 // Reset original Format
439 $this->image->setImageFormat( $orig_format );
441 catch ( Exception $e ) {
442 return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
445 // Set correct file permissions
446 $stat = stat( dirname( $filename ) );
447 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
448 @ chmod( $filename, $perms );
452 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
453 'width' => $this->size['width'],
454 'height' => $this->size['height'],
455 'mime-type' => $mime_type,
460 * Streams current image to browser.
465 * @param string $mime_type
466 * @return boolean|WP_Error
468 public function stream( $mime_type = null ) {
469 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
472 // Temporarily change format for stream
473 $this->image->setImageFormat( strtoupper( $extension ) );
475 // Output stream of image content
476 header( "Content-Type: $mime_type" );
477 print $this->image->getImageBlob();
479 // Reset Image to original Format
480 $this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
482 catch ( Exception $e ) {
483 return new WP_Error( 'image_stream_error', $e->getMessage() );