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() {
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() {
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 * Processes current image and saves to disk
246 * multiple sizes from single source.
251 * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... }
254 public function multi_resize( $sizes ) {
256 $orig_size = $this->size;
257 $orig_image = $this->image->getImage();
259 foreach ( $sizes as $size => $size_data ) {
260 if ( ! $this->image )
261 $this->image = $orig_image->getImage();
263 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
265 if( ! is_wp_error( $resize_result ) ) {
266 $resized = $this->_save( $this->image );
268 $this->image->clear();
269 $this->image->destroy();
272 if ( ! is_wp_error( $resized ) && $resized ) {
273 unset( $resized['path'] );
274 $metadata[$size] = $resized;
278 $this->size = $orig_size;
281 $this->image = $orig_image;
292 * @param string|int $src The source file or Attachment ID.
293 * @param int $src_x The start x position to crop from.
294 * @param int $src_y The start y position to crop from.
295 * @param int $src_w The width to crop.
296 * @param int $src_h The height to crop.
297 * @param int $dst_w Optional. The destination width.
298 * @param int $dst_h Optional. The destination height.
299 * @param boolean $src_abs Optional. If the source crop points are absolute.
300 * @return boolean|WP_Error
302 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
309 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
310 $this->image->setImagePage( $src_w, $src_h, 0, 0);
312 if ( $dst_w || $dst_h ) {
313 // If destination width/height isn't specified, use same as
314 // width/height from source.
320 $this->image->scaleImage( $dst_w, $dst_h );
321 return $this->update_size();
324 catch ( Exception $e ) {
325 return new WP_Error( 'image_crop_error', $e->getMessage() );
327 return $this->update_size();
331 * Rotates current image counter-clockwise by $angle.
336 * @param float $angle
337 * @return boolean|WP_Error
339 public function rotate( $angle ) {
341 * $angle is 360-$angle because Imagick rotates clockwise
342 * (GD rotates counter-clockwise)
345 $this->image->rotateImage( new ImagickPixel('none'), 360-$angle );
347 catch ( Exception $e ) {
348 return new WP_Error( 'image_rotate_error', $e->getMessage() );
350 return $this->update_size();
354 * Flips current image.
359 * @param boolean $horz Horizontal Flip
360 * @param boolean $vert Vertical Flip
361 * @returns boolean|WP_Error
363 public function flip( $horz, $vert ) {
366 $this->image->flipImage();
369 $this->image->flopImage();
371 catch ( Exception $e ) {
372 return new WP_Error( 'image_flip_error', $e->getMessage() );
378 * Saves current image to file.
383 * @param string $destfilename
384 * @param string $mime_type
385 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
387 public function save( $destfilename = null, $mime_type = null ) {
388 $saved = $this->_save( $this->image, $destfilename, $mime_type );
390 if ( ! is_wp_error( $saved ) ) {
391 $this->file = $saved['path'];
392 $this->mime_type = $saved['mime-type'];
395 $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
397 catch ( Exception $e ) {
398 return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
405 protected function _save( $image, $filename = null, $mime_type = null ) {
406 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
409 $filename = $this->generate_filename( null, null, $extension );
412 // Store initial Format
413 $orig_format = $this->image->getImageFormat();
415 $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
416 $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
418 // Reset original Format
419 $this->image->setImageFormat( $orig_format );
421 catch ( Exception $e ) {
422 return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
425 // Set correct file permissions
426 $stat = stat( dirname( $filename ) );
427 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
428 @ chmod( $filename, $perms );
432 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
433 'width' => $this->size['width'],
434 'height' => $this->size['height'],
435 'mime-type' => $mime_type,
440 * Streams current image to browser.
445 * @param string $mime_type
446 * @return boolean|WP_Error
448 public function stream( $mime_type = null ) {
449 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
452 // Temporarily change format for stream
453 $this->image->setImageFormat( strtoupper( $extension ) );
455 // Output stream of image content
456 header( "Content-Type: $mime_type" );
457 print $this->image->getImageBlob();
459 // Reset Image to original Format
460 $this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
462 catch ( Exception $e ) {
463 return new WP_Error( 'image_stream_error', $e->getMessage() );