3 * WordPress GD Image Editor
6 * @subpackage Image_Editor
10 * WordPress Image Editor Class for Image Manipulation through GD
14 * @subpackage Image_Editor
15 * @uses WP_Image_Editor Extends class
17 class WP_Image_Editor_GD extends WP_Image_Editor {
19 protected $image = false; // GD Resource
21 function __destruct() {
23 // we don't need the original in memory anymore
24 imagedestroy( $this->image );
29 * Checks to see if current environment supports GD.
36 public static function test( $args = array() ) {
37 if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
40 // On some setups GD library does not provide imagerotate() - Ticket #11536
41 if ( isset( $args['methods'] ) &&
42 in_array( 'rotate', $args['methods'] ) &&
43 ! function_exists('imagerotate') ){
52 * Checks to see if editor supports the mime-type specified.
57 * @param string $mime_type
60 public static function supports_mime_type( $mime_type ) {
61 $image_types = imagetypes();
62 switch( $mime_type ) {
64 return ($image_types & IMG_JPG) != 0;
66 return ($image_types & IMG_PNG) != 0;
68 return ($image_types & IMG_GIF) != 0;
75 * Loads image from $this->file into new GD Resource.
80 * @return boolean|\WP_Error
82 public function load() {
86 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
87 return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
89 // Set artificially high because GD uses uncompressed images in memory
90 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
91 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
93 if ( ! is_resource( $this->image ) )
94 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file );
96 $size = @getimagesize( $this->file );
98 return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
100 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
101 imagealphablending( $this->image, false );
102 imagesavealpha( $this->image, true );
105 $this->update_size( $size[0], $size[1] );
106 $this->mime_type = $size['mime'];
112 * Sets or updates current image size.
120 protected function update_size( $width = false, $height = false ) {
122 $width = imagesx( $this->image );
125 $height = imagesy( $this->image );
127 return parent::update_size( $width, $height );
131 * Resizes current image.
132 * Wraps _resize, since _resize returns a GD Resource.
139 * @param boolean $crop
140 * @return boolean|WP_Error
142 public function resize( $max_w, $max_h, $crop = false ) {
143 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
146 $resized = $this->_resize( $max_w, $max_h, $crop );
148 if ( is_resource( $resized ) ) {
149 imagedestroy( $this->image );
150 $this->image = $resized;
153 } elseif ( is_wp_error( $resized ) )
156 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
159 protected function _resize( $max_w, $max_h, $crop = false ) {
160 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
162 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
164 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
166 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
167 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
169 if ( is_resource( $resized ) ) {
170 $this->update_size( $dst_w, $dst_h );
174 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
178 * Processes current image and saves to disk
179 * multiple sizes from single source.
181 * 'width' and 'height' are required.
182 * 'crop' defaults to false when not provided.
187 * @param array $sizes { {'width'=>int, 'height'=>int, ['crop'=>bool]}, ... }
190 public function multi_resize( $sizes ) {
192 $orig_size = $this->size;
194 foreach ( $sizes as $size => $size_data ) {
195 if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )
198 if ( ! isset( $size_data['crop'] ) )
199 $size_data['crop'] = false;
201 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
203 if( ! is_wp_error( $image ) ) {
204 $resized = $this->_save( $image );
206 imagedestroy( $image );
208 if ( ! is_wp_error( $resized ) && $resized ) {
209 unset( $resized['path'] );
210 $metadata[$size] = $resized;
214 $this->size = $orig_size;
226 * @param string|int $src The source file or Attachment ID.
227 * @param int $src_x The start x position to crop from.
228 * @param int $src_y The start y position to crop from.
229 * @param int $src_w The width to crop.
230 * @param int $src_h The height to crop.
231 * @param int $dst_w Optional. The destination width.
232 * @param int $dst_h Optional. The destination height.
233 * @param boolean $src_abs Optional. If the source crop points are absolute.
234 * @return boolean|WP_Error
236 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
237 // If destination width/height isn't specified, use same as
238 // width/height from source.
244 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
251 if ( function_exists( 'imageantialias' ) )
252 imageantialias( $dst, true );
254 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
256 if ( is_resource( $dst ) ) {
257 imagedestroy( $this->image );
259 $this->update_size();
263 return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file );
267 * Rotates current image counter-clockwise by $angle.
268 * Ported from image-edit.php
273 * @param float $angle
274 * @return boolean|WP_Error
276 public function rotate( $angle ) {
277 if ( function_exists('imagerotate') ) {
278 $rotated = imagerotate( $this->image, $angle, 0 );
280 if ( is_resource( $rotated ) ) {
281 imagedestroy( $this->image );
282 $this->image = $rotated;
283 $this->update_size();
287 return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file );
291 * Flips current image.
296 * @param boolean $horz Flip along Horizontal Axis
297 * @param boolean $vert Flip along Vertical Axis
298 * @returns boolean|WP_Error
300 public function flip( $horz, $vert ) {
301 $w = $this->size['width'];
302 $h = $this->size['height'];
303 $dst = wp_imagecreatetruecolor( $w, $h );
305 if ( is_resource( $dst ) ) {
306 $sx = $vert ? ($w - 1) : 0;
307 $sy = $horz ? ($h - 1) : 0;
308 $sw = $vert ? -$w : $w;
309 $sh = $horz ? -$h : $h;
311 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
312 imagedestroy( $this->image );
317 return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file );
321 * Saves current in-memory image to file.
326 * @param string $destfilename
327 * @param string $mime_type
328 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
330 public function save( $filename = null, $mime_type = null ) {
331 $saved = $this->_save( $this->image, $filename, $mime_type );
333 if ( ! is_wp_error( $saved ) ) {
334 $this->file = $saved['path'];
335 $this->mime_type = $saved['mime-type'];
341 protected function _save( $image, $filename = null, $mime_type = null ) {
342 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
345 $filename = $this->generate_filename( null, null, $extension );
347 if ( 'image/gif' == $mime_type ) {
348 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) )
349 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
351 elseif ( 'image/png' == $mime_type ) {
352 // convert from full colors to index colors, like original PNG.
353 if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) )
354 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
356 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) )
357 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
359 elseif ( 'image/jpeg' == $mime_type ) {
360 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) )
361 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
364 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
367 // Set correct file permissions
368 $stat = stat( dirname( $filename ) );
369 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
370 @ chmod( $filename, $perms );
374 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
375 'width' => $this->size['width'],
376 'height' => $this->size['height'],
377 'mime-type'=> $mime_type,
382 * Returns stream of current image.
387 * @param string $mime_type
389 public function stream( $mime_type = null ) {
390 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
392 switch ( $mime_type ) {
394 header( 'Content-Type: image/png' );
395 return imagepng( $this->image );
397 header( 'Content-Type: image/gif' );
398 return imagegif( $this->image );
400 header( 'Content-Type: image/jpeg' );
401 return imagejpeg( $this->image, null, $this->quality );
406 * Either calls editor's save function or handles file as a stream.
411 * @param string|stream $filename
412 * @param callable $function
413 * @param array $arguments
416 protected function make_image( $filename, $function, $arguments ) {
417 if ( wp_is_stream( $filename ) )
418 $arguments[1] = null;
420 return parent::make_image( $filename, $function, $arguments );