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 {
21 protected $image; // Imagick Object
23 public function __destruct() {
24 if ( $this->image instanceof Imagick ) {
25 // we don't need the original in memory anymore
26 $this->image->clear();
27 $this->image->destroy();
32 * Checks to see if current environment supports Imagick.
34 * We require Imagick 2.2.0 or greater, based on whether the queryFormats()
35 * method can be called statically.
42 public static function test( $args = array() ) {
44 // First, test Imagick's extension and classes.
45 if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick' ) || ! class_exists( 'ImagickPixel' ) )
48 if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) )
51 $required_methods = array(
61 'setimagecompression',
62 'setimagecompressionquality',
71 // Now, test for deep requirements within Imagick.
72 if ( ! defined( 'imagick::COMPRESSION_JPEG' ) )
75 if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) )
82 * Checks to see if editor supports the mime-type specified.
87 * @param string $mime_type
90 public static function supports_mime_type( $mime_type ) {
91 $imagick_extension = strtoupper( self::get_extension( $mime_type ) );
93 if ( ! $imagick_extension )
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' )
102 return ( (bool) @Imagick::queryFormats( $imagick_extension ) );
104 catch ( Exception $e ) {
110 * Loads image from $this->file into new Imagick Object.
115 * @return boolean|WP_Error True if loaded; WP_Error on failure.
117 public function load() {
118 if ( $this->image instanceof Imagick )
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 );
124 /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
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 ) );
129 $this->image = new Imagick( $this->file );
131 if( ! $this->image->valid() )
132 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
134 // Select the first frame to handle animated images properly
135 if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
136 $this->image->setIteratorIndex(0);
138 $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
140 catch ( Exception $e ) {
141 return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
144 $updated_size = $this->update_size();
145 if ( is_wp_error( $updated_size ) ) {
146 return $updated_size;
149 return $this->set_quality();
153 * Sets Image Compression quality on a 1-100% scale.
158 * @param int $quality Compression Quality. Range: [1,100]
159 * @return boolean|WP_Error True if set successfully; WP_Error on failure.
161 public function set_quality( $quality = null ) {
162 $quality_result = parent::set_quality( $quality );
163 if ( is_wp_error( $quality_result ) ) {
164 return $quality_result;
166 $quality = $this->get_quality();
170 if ( 'image/jpeg' == $this->mime_type ) {
171 $this->image->setImageCompressionQuality( $quality );
172 $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
175 $this->image->setImageCompressionQuality( $quality );
178 catch ( Exception $e ) {
179 return new WP_Error( 'image_quality_error', $e->getMessage() );
186 * Sets or updates current image size.
194 * @return true|WP_Error
196 protected function update_size( $width = null, $height = null ) {
198 if ( !$width || !$height ) {
200 $size = $this->image->getImageGeometry();
202 catch ( Exception $e ) {
203 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file );
208 $width = $size['width'];
211 $height = $size['height'];
213 return parent::update_size( $width, $height );
217 * Resizes current image.
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.
226 * @param int|null $max_w Image width.
227 * @param int|null $max_h Image height.
228 * @param boolean $crop
229 * @return boolean|WP_Error
231 public function resize( $max_w, $max_h, $crop = false ) {
232 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
235 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
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;
241 return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
246 * @TODO: Thumbnail is more efficient, given a newer version of Imagemagick.
247 * $this->image->thumbnailImage( $dst_w, $dst_h );
249 $this->image->scaleImage( $dst_w, $dst_h );
251 catch ( Exception $e ) {
252 return new WP_Error( 'image_resize_error', $e->getMessage() );
255 return $this->update_size( $dst_w, $dst_h );
259 * Resize multiple images from a single source.
264 * @param array $sizes {
265 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
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.
271 * @type array $size {
272 * @type int ['width'] Optional. Image width.
273 * @type int ['height'] Optional. Image height.
274 * @type bool $crop Optional. Whether to crop the image. Default false.
277 * @return array An array of resized images' metadata by size.
279 public function multi_resize( $sizes ) {
281 $orig_size = $this->size;
282 $orig_image = $this->image->getImage();
284 foreach ( $sizes as $size => $size_data ) {
285 if ( ! $this->image )
286 $this->image = $orig_image->getImage();
288 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
292 if ( ! isset( $size_data['width'] ) ) {
293 $size_data['width'] = null;
295 if ( ! isset( $size_data['height'] ) ) {
296 $size_data['height'] = null;
299 if ( ! isset( $size_data['crop'] ) ) {
300 $size_data['crop'] = false;
303 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
304 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
306 if ( ! is_wp_error( $resize_result ) && ! $duplicate ) {
307 $resized = $this->_save( $this->image );
309 $this->image->clear();
310 $this->image->destroy();
313 if ( ! is_wp_error( $resized ) && $resized ) {
314 unset( $resized['path'] );
315 $metadata[$size] = $resized;
319 $this->size = $orig_size;
322 $this->image = $orig_image;
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
342 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
349 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
350 $this->image->setImagePage( $src_w, $src_h, 0, 0);
352 if ( $dst_w || $dst_h ) {
353 // If destination width/height isn't specified, use same as
354 // width/height from source.
360 $this->image->scaleImage( $dst_w, $dst_h );
361 return $this->update_size();
364 catch ( Exception $e ) {
365 return new WP_Error( 'image_crop_error', $e->getMessage() );
367 return $this->update_size();
371 * Rotates current image counter-clockwise by $angle.
376 * @param float $angle
377 * @return boolean|WP_Error
379 public function rotate( $angle ) {
381 * $angle is 360-$angle because Imagick rotates clockwise
382 * (GD rotates counter-clockwise)
385 $this->image->rotateImage( new ImagickPixel('none'), 360-$angle );
387 // Since this changes the dimensions of the image, update the size.
388 $result = $this->update_size();
389 if ( is_wp_error( $result ) )
392 $this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 );
394 catch ( Exception $e ) {
395 return new WP_Error( 'image_rotate_error', $e->getMessage() );
401 * Flips current image.
406 * @param boolean $horz Flip along Horizontal Axis
407 * @param boolean $vert Flip along Vertical Axis
408 * @returns boolean|WP_Error
410 public function flip( $horz, $vert ) {
413 $this->image->flipImage();
416 $this->image->flopImage();
418 catch ( Exception $e ) {
419 return new WP_Error( 'image_flip_error', $e->getMessage() );
425 * Saves current image to file.
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}
434 public function save( $destfilename = null, $mime_type = null ) {
435 $saved = $this->_save( $this->image, $destfilename, $mime_type );
437 if ( ! is_wp_error( $saved ) ) {
438 $this->file = $saved['path'];
439 $this->mime_type = $saved['mime-type'];
442 $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
444 catch ( Exception $e ) {
445 return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
452 protected function _save( $image, $filename = null, $mime_type = null ) {
453 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
456 $filename = $this->generate_filename( null, null, $extension );
459 // Store initial Format
460 $orig_format = $this->image->getImageFormat();
462 $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
463 $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
465 // Reset original Format
466 $this->image->setImageFormat( $orig_format );
468 catch ( Exception $e ) {
469 return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
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 );
477 /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
480 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
481 'width' => $this->size['width'],
482 'height' => $this->size['height'],
483 'mime-type' => $mime_type,
488 * Streams current image to browser.
493 * @param string $mime_type
494 * @return boolean|WP_Error
496 public function stream( $mime_type = null ) {
497 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
500 // Temporarily change format for stream
501 $this->image->setImageFormat( strtoupper( $extension ) );
503 // Output stream of image content
504 header( "Content-Type: $mime_type" );
505 print $this->image->getImageBlob();
507 // Reset Image to original Format
508 $this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
510 catch ( Exception $e ) {
511 return new WP_Error( 'image_stream_error', $e->getMessage() );