]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-image-editor-imagick.php
Wordpress 3.5.2
[autoinstalls/wordpress.git] / wp-includes / class-wp-image-editor-imagick.php
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 {
18
19         protected $image = null; // Imagick Object
20
21         function __destruct() {
22                 if ( $this->image ) {
23                         // we don't need the original in memory anymore
24                         $this->image->clear();
25                         $this->image->destroy();
26                 }
27         }
28
29         /**
30          * Checks to see if current environment supports Imagick.
31          *
32          * We require Imagick 2.2.0 or greater, based on whether the queryFormats()
33          * method can be called statically.
34          *
35          * @since 3.5.0
36          * @access public
37          *
38          * @return boolean
39          */
40         public static function test( $args = array() ) {
41
42                 // First, test Imagick's extension and classes.
43                 if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick' ) || ! class_exists( 'ImagickPixel' ) )
44                         return false;
45
46                 if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) )
47                         return false;
48
49                 $required_methods = array(
50                         'clear',
51                         'destroy',
52                         'valid',
53                         'getimage',
54                         'writeimage',
55                         'getimageblob',
56                         'getimagegeometry',
57                         'getimageformat',
58                         'setimageformat',
59                         'setimagecompression',
60                         'setimagecompressionquality',
61                         'setimagepage',
62                         'scaleimage',
63                         'cropimage',
64                         'rotateimage',
65                         'flipimage',
66                         'flopimage',
67                 );
68
69                 // Now, test for deep requirements within Imagick.
70                 if ( ! defined( 'imagick::COMPRESSION_JPEG' ) )
71                         return false;
72
73                 if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) )
74                         return false;
75
76                 return true;
77         }
78
79         /**
80          * Checks to see if editor supports the mime-type specified.
81          *
82          * @since 3.5.0
83          * @access public
84          *
85          * @param string $mime_type
86          * @return boolean
87          */
88         public static function supports_mime_type( $mime_type ) {
89                 $imagick_extension = strtoupper( self::get_extension( $mime_type ) );
90
91                 if ( ! $imagick_extension )
92                         return false;
93
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' )
97                                 return false;
98
99                 try {
100                         return ( (bool) Imagick::queryFormats( $imagick_extension ) );
101                 }
102                 catch ( Exception $e ) {
103                         return false;
104                 }
105         }
106
107         /**
108          * Loads image from $this->file into new Imagick Object.
109          *
110          * @since 3.5.0
111          * @access protected
112          *
113          * @return boolean|WP_Error True if loaded; WP_Error on failure.
114          */
115         public function load() {
116                 if ( $this->image )
117                         return true;
118
119                 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
120                         return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
121
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 ) );
124
125                 try {
126                         $this->image = new Imagick( $this->file );
127
128                         if( ! $this->image->valid() )
129                                 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
130
131                         // Select the first frame to handle animated images properly
132                         if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
133                                 $this->image->setIteratorIndex(0);
134
135                         $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
136                 }
137                 catch ( Exception $e ) {
138                         return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
139                 }
140
141                 $updated_size = $this->update_size();
142                 if ( is_wp_error( $updated_size ) )
143                                 return $updated_size;
144
145                 return $this->set_quality();
146         }
147
148         /**
149          * Sets Image Compression quality on a 1-100% scale.
150          *
151          * @since 3.5.0
152          * @access public
153          *
154          * @param int $quality Compression Quality. Range: [1,100]
155          * @return boolean|WP_Error
156          */
157         public function set_quality( $quality = null ) {
158                 if ( !$quality )
159                         $quality = $this->quality;
160
161                 try {
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 );
165                         }
166                         else {
167                                 $this->image->setImageCompressionQuality( $quality );
168                         }
169                 }
170                 catch ( Exception $e ) {
171                         return new WP_Error( 'image_quality_error', $e->getMessage() );
172                 }
173
174                 return parent::set_quality( $quality );
175         }
176
177         /**
178          * Sets or updates current image size.
179          *
180          * @since 3.5.0
181          * @access protected
182          *
183          * @param int $width
184          * @param int $height
185          */
186         protected function update_size( $width = null, $height = null ) {
187                 $size = null;
188                 if ( !$width || !$height ) {
189                         try {
190                                 $size = $this->image->getImageGeometry();
191                         }
192                         catch ( Exception $e ) {
193                                 return new WP_Error( 'invalid_image', __('Could not read image size'), $this->file );
194                         }
195                 }
196
197                 if ( ! $width )
198                         $width = $size['width'];
199
200                 if ( ! $height )
201                         $height = $size['height'];
202
203                 return parent::update_size( $width, $height );
204         }
205
206         /**
207          * Resizes current image.
208          *
209          * @since 3.5.0
210          * @access public
211          *
212          * @param int $max_w
213          * @param int $max_h
214          * @param boolean $crop
215          * @return boolean|WP_Error
216          */
217         public function resize( $max_w, $max_h, $crop = false ) {
218                 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
219                         return true;
220
221                 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
222                 if ( ! $dims )
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;
225
226                 if ( $crop ) {
227                         return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
228                 }
229
230                 try {
231                         /**
232                          * @TODO: Thumbnail is more efficient, given a newer version of Imagemagick.
233                          * $this->image->thumbnailImage( $dst_w, $dst_h );
234                          */
235                         $this->image->scaleImage( $dst_w, $dst_h );
236                 }
237                 catch ( Exception $e ) {
238                         return new WP_Error( 'image_resize_error', $e->getMessage() );
239                 }
240
241                 return $this->update_size( $dst_w, $dst_h );
242         }
243
244         /**
245          * Processes current image and saves to disk
246          * multiple sizes from single source.
247          *
248          * @since 3.5.0
249          * @access public
250          *
251          * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... }
252          * @return array
253          */
254         public function multi_resize( $sizes ) {
255                 $metadata = array();
256                 $orig_size = $this->size;
257                 $orig_image = $this->image->getImage();
258
259                 foreach ( $sizes as $size => $size_data ) {
260                         if ( ! $this->image )
261                                 $this->image = $orig_image->getImage();
262
263                         $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
264
265                         if( ! is_wp_error( $resize_result ) ) {
266                                 $resized = $this->_save( $this->image );
267
268                                 $this->image->clear();
269                                 $this->image->destroy();
270                                 $this->image = null;
271
272                                 if ( ! is_wp_error( $resized ) && $resized ) {
273                                         unset( $resized['path'] );
274                                         $metadata[$size] = $resized;
275                                 }
276                         }
277
278                         $this->size = $orig_size;
279                 }
280
281                 $this->image = $orig_image;
282
283                 return $metadata;
284         }
285
286         /**
287          * Crops Image.
288          *
289          * @since 3.5.0
290          * @access public
291          *
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
301          */
302         public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
303                 if ( $src_abs ) {
304                         $src_w -= $src_x;
305                         $src_h -= $src_y;
306                 }
307
308                 try {
309                         $this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
310                         $this->image->setImagePage( $src_w, $src_h, 0, 0);
311
312                         if ( $dst_w || $dst_h ) {
313                                 // If destination width/height isn't specified, use same as
314                                 // width/height from source.
315                                 if ( ! $dst_w )
316                                         $dst_w = $src_w;
317                                 if ( ! $dst_h )
318                                         $dst_h = $src_h;
319
320                                 $this->image->scaleImage( $dst_w, $dst_h );
321                                 return $this->update_size();
322                         }
323                 }
324                 catch ( Exception $e ) {
325                         return new WP_Error( 'image_crop_error', $e->getMessage() );
326                 }
327                 return $this->update_size();
328         }
329
330         /**
331          * Rotates current image counter-clockwise by $angle.
332          *
333          * @since 3.5.0
334          * @access public
335          *
336          * @param float $angle
337          * @return boolean|WP_Error
338          */
339         public function rotate( $angle ) {
340                 /**
341                  * $angle is 360-$angle because Imagick rotates clockwise
342                  * (GD rotates counter-clockwise)
343                  */
344                 try {
345                         $this->image->rotateImage( new ImagickPixel('none'), 360-$angle );
346                 }
347                 catch ( Exception $e ) {
348                         return new WP_Error( 'image_rotate_error', $e->getMessage() );
349                 }
350                 return $this->update_size();
351         }
352
353         /**
354          * Flips current image.
355          *
356          * @since 3.5.0
357          * @access public
358          *
359          * @param boolean $horz Horizontal Flip
360          * @param boolean $vert Vertical Flip
361          * @returns boolean|WP_Error
362          */
363         public function flip( $horz, $vert ) {
364                 try {
365                         if ( $horz )
366                                 $this->image->flipImage();
367
368                         if ( $vert )
369                                 $this->image->flopImage();
370                 }
371                 catch ( Exception $e ) {
372                         return new WP_Error( 'image_flip_error', $e->getMessage() );
373                 }
374                 return true;
375         }
376
377         /**
378          * Saves current image to file.
379          *
380          * @since 3.5.0
381          * @access public
382          *
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}
386          */
387         public function save( $destfilename = null, $mime_type = null ) {
388                 $saved = $this->_save( $this->image, $destfilename, $mime_type );
389
390                 if ( ! is_wp_error( $saved ) ) {
391                         $this->file = $saved['path'];
392                         $this->mime_type = $saved['mime-type'];
393
394                         try {
395                                 $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
396                         }
397                         catch ( Exception $e ) {
398                                 return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
399                         }
400                 }
401
402                 return $saved;
403         }
404
405         protected function _save( $image, $filename = null, $mime_type = null ) {
406                 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
407
408                 if ( ! $filename )
409                         $filename = $this->generate_filename( null, null, $extension );
410
411                 try {
412                         // Store initial Format
413                         $orig_format = $this->image->getImageFormat();
414
415                         $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
416                         $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
417
418                         // Reset original Format
419                         $this->image->setImageFormat( $orig_format );
420                 }
421                 catch ( Exception $e ) {
422                         return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
423                 }
424
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 );
429
430                 return array(
431                         'path' => $filename,
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,
436                 );
437         }
438
439         /**
440          * Streams current image to browser.
441          *
442          * @since 3.5.0
443          * @access public
444          *
445          * @param string $mime_type
446          * @return boolean|WP_Error
447          */
448         public function stream( $mime_type = null ) {
449                 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
450
451                 try {
452                         // Temporarily change format for stream
453                         $this->image->setImageFormat( strtoupper( $extension ) );
454
455                         // Output stream of image content
456                         header( "Content-Type: $mime_type" );
457                         print $this->image->getImageBlob();
458
459                         // Reset Image to original Format
460                         $this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
461                 }
462                 catch ( Exception $e ) {
463                         return new WP_Error( 'image_stream_error', $e->getMessage() );
464                 }
465
466                 return true;
467         }
468 }