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 {
26 public function __destruct() {
28 // we don't need the original in memory anymore
29 imagedestroy( $this->image );
34 * Checks to see if current environment supports GD.
44 public static function test( $args = array() ) {
45 if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
48 // On some setups GD library does not provide imagerotate() - Ticket #11536
49 if ( isset( $args['methods'] ) &&
50 in_array( 'rotate', $args['methods'] ) &&
51 ! function_exists('imagerotate') ){
60 * Checks to see if editor supports the mime-type specified.
67 * @param string $mime_type
70 public static function supports_mime_type( $mime_type ) {
71 $image_types = imagetypes();
72 switch( $mime_type ) {
74 return ($image_types & IMG_JPG) != 0;
76 return ($image_types & IMG_PNG) != 0;
78 return ($image_types & IMG_GIF) != 0;
85 * Loads image from $this->file into new GD Resource.
90 * @return bool|WP_Error True if loaded successfully; WP_Error on failure.
92 public function load() {
96 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
97 return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
100 * Filter the memory limit allocated for image manipulation.
104 * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT.
105 * Accepts an integer (bytes), or a shorthand string notation, such as '256M'.
107 // Set artificially high because GD uses uncompressed images in memory
108 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
110 $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
112 if ( ! is_resource( $this->image ) )
113 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file );
115 $size = @getimagesize( $this->file );
117 return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
119 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
120 imagealphablending( $this->image, false );
121 imagesavealpha( $this->image, true );
124 $this->update_size( $size[0], $size[1] );
125 $this->mime_type = $size['mime'];
127 return $this->set_quality();
131 * Sets or updates current image size.
140 protected function update_size( $width = false, $height = false ) {
142 $width = imagesx( $this->image );
145 $height = imagesy( $this->image );
147 return parent::update_size( $width, $height );
151 * Resizes current image.
152 * Wraps _resize, since _resize returns a GD Resource.
154 * At minimum, either a height or width must be provided.
155 * If one of the two is set to null, the resize will
156 * maintain aspect ratio according to the provided dimension.
161 * @param int|null $max_w Image width.
162 * @param int|null $max_h Image height.
164 * @return true|WP_Error
166 public function resize( $max_w, $max_h, $crop = false ) {
167 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
170 $resized = $this->_resize( $max_w, $max_h, $crop );
172 if ( is_resource( $resized ) ) {
173 imagedestroy( $this->image );
174 $this->image = $resized;
177 } elseif ( is_wp_error( $resized ) )
180 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
187 * @param bool|array $crop
188 * @return resource|WP_Error
190 protected function _resize( $max_w, $max_h, $crop = false ) {
191 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
193 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
195 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
197 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
198 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
200 if ( is_resource( $resized ) ) {
201 $this->update_size( $dst_w, $dst_h );
205 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
209 * Resize multiple images from a single source.
214 * @param array $sizes {
215 * An array of image size arrays. Default sizes are 'small', 'medium', 'medium_large', 'large'.
217 * Either a height or width must be provided.
218 * If one of the two is set to null, the resize will
219 * maintain aspect ratio according to the provided dimension.
221 * @type array $size {
222 * Array of height, width values, and whether to crop.
224 * @type int $width Image width. Optional if `$height` is specified.
225 * @type int $height Image height. Optional if `$width` is specified.
226 * @type bool $crop Optional. Whether to crop the image. Default false.
229 * @return array An array of resized images' metadata by size.
231 public function multi_resize( $sizes ) {
233 $orig_size = $this->size;
235 foreach ( $sizes as $size => $size_data ) {
236 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
240 if ( ! isset( $size_data['width'] ) ) {
241 $size_data['width'] = null;
243 if ( ! isset( $size_data['height'] ) ) {
244 $size_data['height'] = null;
247 if ( ! isset( $size_data['crop'] ) ) {
248 $size_data['crop'] = false;
251 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
252 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
254 if ( ! is_wp_error( $image ) && ! $duplicate ) {
255 $resized = $this->_save( $image );
257 imagedestroy( $image );
259 if ( ! is_wp_error( $resized ) && $resized ) {
260 unset( $resized['path'] );
261 $metadata[$size] = $resized;
265 $this->size = $orig_size;
277 * @param int $src_x The start x position to crop from.
278 * @param int $src_y The start y position to crop from.
279 * @param int $src_w The width to crop.
280 * @param int $src_h The height to crop.
281 * @param int $dst_w Optional. The destination width.
282 * @param int $dst_h Optional. The destination height.
283 * @param bool $src_abs Optional. If the source crop points are absolute.
284 * @return bool|WP_Error
286 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
287 // If destination width/height isn't specified, use same as
288 // width/height from source.
294 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
301 if ( function_exists( 'imageantialias' ) )
302 imageantialias( $dst, true );
304 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
306 if ( is_resource( $dst ) ) {
307 imagedestroy( $this->image );
309 $this->update_size();
313 return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file );
317 * Rotates current image counter-clockwise by $angle.
318 * Ported from image-edit.php
323 * @param float $angle
324 * @return true|WP_Error
326 public function rotate( $angle ) {
327 if ( function_exists('imagerotate') ) {
328 $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
329 $rotated = imagerotate( $this->image, $angle, $transparency );
331 if ( is_resource( $rotated ) ) {
332 imagealphablending( $rotated, true );
333 imagesavealpha( $rotated, true );
334 imagedestroy( $this->image );
335 $this->image = $rotated;
336 $this->update_size();
340 return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file );
344 * Flips current image.
349 * @param bool $horz Flip along Horizontal Axis
350 * @param bool $vert Flip along Vertical Axis
351 * @return true|WP_Error
353 public function flip( $horz, $vert ) {
354 $w = $this->size['width'];
355 $h = $this->size['height'];
356 $dst = wp_imagecreatetruecolor( $w, $h );
358 if ( is_resource( $dst ) ) {
359 $sx = $vert ? ($w - 1) : 0;
360 $sy = $horz ? ($h - 1) : 0;
361 $sw = $vert ? -$w : $w;
362 $sh = $horz ? -$h : $h;
364 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
365 imagedestroy( $this->image );
370 return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file );
374 * Saves current in-memory image to file.
379 * @param string|null $filename
380 * @param string|null $mime_type
381 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
383 public function save( $filename = null, $mime_type = null ) {
384 $saved = $this->_save( $this->image, $filename, $mime_type );
386 if ( ! is_wp_error( $saved ) ) {
387 $this->file = $saved['path'];
388 $this->mime_type = $saved['mime-type'];
395 * @param resource $image
396 * @param string|null $filename
397 * @param string|null $mime_type
398 * @return WP_Error|array
400 protected function _save( $image, $filename = null, $mime_type = null ) {
401 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
404 $filename = $this->generate_filename( null, null, $extension );
406 if ( 'image/gif' == $mime_type ) {
407 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) )
408 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
410 elseif ( 'image/png' == $mime_type ) {
411 // convert from full colors to index colors, like original PNG.
412 if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) )
413 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
415 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) )
416 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
418 elseif ( 'image/jpeg' == $mime_type ) {
419 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) )
420 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
423 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
426 // Set correct file permissions
427 $stat = stat( dirname( $filename ) );
428 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
429 @ chmod( $filename, $perms );
432 * Filter the name of the saved image file.
436 * @param string $filename Name of the file.
440 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
441 'width' => $this->size['width'],
442 'height' => $this->size['height'],
443 'mime-type' => $mime_type,
448 * Returns stream of current image.
453 * @param string $mime_type
456 public function stream( $mime_type = null ) {
457 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
459 switch ( $mime_type ) {
461 header( 'Content-Type: image/png' );
462 return imagepng( $this->image );
464 header( 'Content-Type: image/gif' );
465 return imagegif( $this->image );
467 header( 'Content-Type: image/jpeg' );
468 return imagejpeg( $this->image, null, $this->get_quality() );
473 * Either calls editor's save function or handles file as a stream.
478 * @param string|stream $filename
479 * @param callable $function
480 * @param array $arguments
483 protected function make_image( $filename, $function, $arguments ) {
484 if ( wp_is_stream( $filename ) )
485 $arguments[1] = null;
487 return parent::make_image( $filename, $function, $arguments );