1<?php
2/**
3 * WordPress GD Image Editor
4 *
5 * @package WordPress
6 * @subpackage Image_Editor
7 */
8
9/**
10 * WordPress Image Editor Class for Image Manipulation through GD
11 *
12 * @since 3.5.0
13 *
14 * @see WP_Image_Editor
15 */
16class WP_Image_Editor_GD extends WP_Image_Editor {
17 /**
18 * GD Resource.
19 *
20 * @var resource|GdImage
21 */
22 protected $image;
23
24 public function __destruct() {
25 if ( $this->image ) {
26 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
27 // We don't need the original in memory anymore.
28 imagedestroy( $this->image );
29 }
30 }
31 }
32
33 /**
34 * Checks to see if current environment supports GD.
35 *
36 * @since 3.5.0
37 *
38 * @param array $args
39 * @return bool
40 */
41 public static function test( $args = array() ) {
42 if ( ! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' ) ) {
43 return false;
44 }
45
46 // On some setups GD library does not provide imagerotate() - Ticket #11536.
47 if ( isset( $args['methods'] ) &&
48 in_array( 'rotate', $args['methods'], true ) &&
49 ! function_exists( 'imagerotate' ) ) {
50
51 return false;
52 }
53
54 return true;
55 }
56
57 /**
58 * Checks to see if editor supports the mime-type specified.
59 *
60 * @since 3.5.0
61 *
62 * @param string $mime_type
63 * @return bool
64 */
65 public static function supports_mime_type( $mime_type ) {
66 $image_types = imagetypes();
67 switch ( $mime_type ) {
68 case 'image/jpeg':
69 return ( $image_types & IMG_JPG ) !== 0;
70 case 'image/png':
71 return ( $image_types & IMG_PNG ) !== 0;
72 case 'image/gif':
73 return ( $image_types & IMG_GIF ) !== 0;
74 case 'image/webp':
75 return ( $image_types & IMG_WEBP ) !== 0;
76 case 'image/avif':
77 return ( $image_types & IMG_AVIF ) !== 0 && function_exists( 'imageavif' );
78 }
79
80 return false;
81 }
82
83 /**
84 * Loads image from $this->file into new GD Resource.
85 *
86 * @since 3.5.0
87 *
88 * @return true|WP_Error True if loaded successfully; WP_Error on failure.
89 */
90 public function load() {
91 if ( $this->image ) {
92 return true;
93 }
94
95 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
96 return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
97 }
98
99 // Set artificially high because GD uses uncompressed images in memory.
100 wp_raise_memory_limit( 'image' );
101
102 $file_contents = @file_get_contents( $this->file );
103
104 if ( ! $file_contents ) {
105 return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
106 }
107
108 // Handle WebP and AVIF mime types explicitly, falling back to imagecreatefromstring.
109 if (
110 function_exists( 'imagecreatefromwebp' ) && ( 'image/webp' === wp_get_image_mime( $this->file ) )
111 ) {
112 $this->image = @imagecreatefromwebp( $this->file );
113 } elseif (
114 function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $this->file ) )
115 ) {
116 $this->image = @imagecreatefromavif( $this->file );
117 } else {
118 $this->image = @imagecreatefromstring( $file_contents );
119 }
120
121 if ( ! is_gd_image( $this->image ) ) {
122 return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
123 }
124
125 $size = wp_getimagesize( $this->file );
126
127 if ( ! $size ) {
128 return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
129 }
130
131 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
132 imagealphablending( $this->image, false );
133 imagesavealpha( $this->image, true );
134 }
135
136 $this->update_size( $size[0], $size[1] );
137 $this->mime_type = $size['mime'];
138
139 return $this->set_quality();
140 }
141
142 /**
143 * Sets or updates current image size.
144 *
145 * @since 3.5.0
146 *
147 * @param int $width
148 * @param int $height
149 * @return true
150 */
151 protected function update_size( $width = false, $height = false ) {
152 if ( ! $width ) {
153 $width = imagesx( $this->image );
154 }
155
156 if ( ! $height ) {
157 $height = imagesy( $this->image );
158 }
159
160 return parent::update_size( $width, $height );
161 }
162
163 /**
164 * Resizes current image.
165 *
166 * Wraps `::_resize()` which returns a GD resource or GdImage instance.
167 *
168 * At minimum, either a height or width must be provided. If one of the two is set
169 * to null, the resize will maintain aspect ratio according to the provided dimension.
170 *
171 * @since 3.5.0
172 *
173 * @param int|null $max_w Image width.
174 * @param int|null $max_h Image height.
175 * @param bool|array $crop {
176 * Optional. Image cropping behavior. If false, the image will be scaled (default).
177 * If true, image will be cropped to the specified dimensions using center positions.
178 * If an array, the image will be cropped using the array to specify the crop location:
179 *
180 * @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.
181 * @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
182 * }
183 * @return true|WP_Error
184 */
185 public function resize( $max_w, $max_h, $crop = false ) {
186 if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) {
187 return true;
188 }
189
190 $resized = $this->_resize( $max_w, $max_h, $crop );
191
192 if ( is_gd_image( $resized ) ) {
193 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
194 imagedestroy( $this->image );
195 }
196
197 $this->image = $resized;
198
199 return true;
200
201 } elseif ( is_wp_error( $resized ) ) {
202 return $resized;
203 }
204
205 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
206 }
207
208 /**
209 * @param int $max_w
210 * @param int $max_h
211 * @param bool|array $crop {
212 * Optional. Image cropping behavior. If false, the image will be scaled (default).
213 * If true, image will be cropped to the specified dimensions using center positions.
214 * If an array, the image will be cropped using the array to specify the crop location:
215 *
216 * @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.
217 * @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
218 * }
219 * @return resource|GdImage|WP_Error
220 */
221 protected function _resize( $max_w, $max_h, $crop = false ) {
222 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
223
224 if ( ! $dims ) {
225 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
226 }
227
228 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
229
230 $this->set_quality(
231 null,
232 array(
233 'width' => $dst_w,
234 'height' => $dst_h,
235 )
236 );
237
238 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
239 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
240
241 if ( is_gd_image( $resized ) ) {
242 $this->update_size( $dst_w, $dst_h );
243 return $resized;
244 }
245
246 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
247 }
248
249 /**
250 * Create multiple smaller images from a single source.
251 *
252 * Attempts to create all sub-sizes and returns the meta data at the end. This
253 * may result in the server running out of resources. When it fails there may be few
254 * "orphaned" images left over as the meta data is never returned and saved.
255 *
256 * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates
257 * the new images one at a time and allows for the meta data to be saved after
258 * each new image is created.
259 *
260 * @since 3.5.0
261 *
262 * @param array $sizes {
263 * An array of image size data arrays.
264 *
265 * Either a height or width must be provided.
266 * If one of the two is set to null, the resize will
267 * maintain aspect ratio according to the source image.
268 *
269 * @type array ...$0 {
270 * Array of height, width values, and whether to crop.
271 *
272 * @type int $width Image width. Optional if `$height` is specified.
273 * @type int $height Image height. Optional if `$width` is specified.
274 * @type bool|array $crop Optional. Whether to crop the image. Default false.
275 * }
276 * }
277 * @return array An array of resized images' metadata by size.
278 */
279 public function multi_resize( $sizes ) {
280 $metadata = array();
281
282 foreach ( $sizes as $size => $size_data ) {
283 $meta = $this->make_subsize( $size_data );
284
285 if ( ! is_wp_error( $meta ) ) {
286 $metadata[ $size ] = $meta;
287 }
288 }
289
290 return $metadata;
291 }
292
293 /**
294 * Create an image sub-size and return the image meta data value for it.
295 *
296 * @since 5.3.0
297 *
298 * @param array $size_data {
299 * Array of size data.
300 *
301 * @type int $width The maximum width in pixels.
302 * @type int $height The maximum height in pixels.
303 * @type bool|array $crop Whether to crop the image to exact dimensions.
304 * }
305 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
306 * WP_Error object on error.
307 */
308 public function make_subsize( $size_data ) {
309 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
310 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
311 }
312
313 $orig_size = $this->size;
314
315 if ( ! isset( $size_data['width'] ) ) {
316 $size_data['width'] = null;
317 }
318
319 if ( ! isset( $size_data['height'] ) ) {
320 $size_data['height'] = null;
321 }
322
323 if ( ! isset( $size_data['crop'] ) ) {
324 $size_data['crop'] = false;
325 }
326
327 $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
328
329 if ( is_wp_error( $resized ) ) {
330 $saved = $resized;
331 } else {
332 $saved = $this->_save( $resized );
333
334 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
335 imagedestroy( $resized );
336 }
337 }
338
339 $this->size = $orig_size;
340
341 if ( ! is_wp_error( $saved ) ) {
342 unset( $saved['path'] );
343 }
344
345 return $saved;
346 }
347
348 /**
349 * Crops Image.
350 *
351 * @since 3.5.0
352 *
353 * @param int $src_x The start x position to crop from.
354 * @param int $src_y The start y position to crop from.
355 * @param int $src_w The width to crop.
356 * @param int $src_h The height to crop.
357 * @param int $dst_w Optional. The destination width.
358 * @param int $dst_h Optional. The destination height.
359 * @param bool $src_abs Optional. If the source crop points are absolute.
360 * @return true|WP_Error
361 */
362 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
363 /*
364 * If destination width/height isn't specified,
365 * use same as width/height from source.
366 */
367 if ( ! $dst_w ) {
368 $dst_w = $src_w;
369 }
370 if ( ! $dst_h ) {
371 $dst_h = $src_h;
372 }
373
374 foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) {
375 if ( ! is_numeric( $value ) || (int) $value <= 0 ) {
376 return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
377 }
378 }
379
380 $dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h );
381
382 if ( $src_abs ) {
383 $src_w -= $src_x;
384 $src_h -= $src_y;
385 }
386
387 if ( function_exists( 'imageantialias' ) ) {
388 imageantialias( $dst, true );
389 }
390
391 imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h );
392
393 if ( is_gd_image( $dst ) ) {
394 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
395 imagedestroy( $this->image );
396 }
397
398 $this->image = $dst;
399 $this->update_size();
400
401 return true;
402 }
403
404 return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
405 }
406
407 /**
408 * Rotates current image counter-clockwise by $angle.
409 * Ported from image-edit.php
410 *
411 * @since 3.5.0
412 *
413 * @param float $angle
414 * @return true|WP_Error
415 */
416 public function rotate( $angle ) {
417 if ( function_exists( 'imagerotate' ) ) {
418 $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
419 $rotated = imagerotate( $this->image, $angle, $transparency );
420
421 if ( is_gd_image( $rotated ) ) {
422 imagealphablending( $rotated, true );
423 imagesavealpha( $rotated, true );
424
425 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
426 imagedestroy( $this->image );
427 }
428
429 $this->image = $rotated;
430 $this->update_size();
431
432 return true;
433 }
434 }
435
436 return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file );
437 }
438
439 /**
440 * Flips current image.
441 *
442 * @since 3.5.0
443 *
444 * @param bool $horz Flip along Horizontal Axis.
445 * @param bool $vert Flip along Vertical Axis.
446 * @return true|WP_Error
447 */
448 public function flip( $horz, $vert ) {
449 $w = $this->size['width'];
450 $h = $this->size['height'];
451 $dst = wp_imagecreatetruecolor( $w, $h );
452
453 if ( is_gd_image( $dst ) ) {
454 $sx = $vert ? ( $w - 1 ) : 0;
455 $sy = $horz ? ( $h - 1 ) : 0;
456 $sw = $vert ? -$w : $w;
457 $sh = $horz ? -$h : $h;
458
459 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
460 if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
461 imagedestroy( $this->image );
462 }
463
464 $this->image = $dst;
465
466 return true;
467 }
468 }
469
470 return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
471 }
472
473 /**
474 * Saves current in-memory image to file.
475 *
476 * @since 3.5.0
477 * @since 5.9.0 Renamed `$filename` to `$destfilename` to match parent class
478 * for PHP 8 named parameter support.
479 * @since 6.0.0 The `$filesize` value was added to the returned array.
480 *
481 * @param string|null $destfilename Optional. Destination filename. Default null.
482 * @param string|null $mime_type Optional. The mime-type. Default null.
483 * @return array|WP_Error {
484 * Array on success or WP_Error if the file failed to save.
485 *
486 * @type string $path Path to the image file.
487 * @type string $file Name of the image file.
488 * @type int $width Image width.
489 * @type int $height Image height.
490 * @type string $mime-type The mime type of the image.
491 * @type int $filesize File size of the image.
492 * }
493 */
494 public function save( $destfilename = null, $mime_type = null ) {
495 $saved = $this->_save( $this->image, $destfilename, $mime_type );
496
497 if ( ! is_wp_error( $saved ) ) {
498 $this->file = $saved['path'];
499 $this->mime_type = $saved['mime-type'];
500 }
501
502 return $saved;
503 }
504
505 /**
506 * @since 3.5.0
507 * @since 6.0.0 The `$filesize` value was added to the returned array.
508 *
509 * @param resource|GdImage $image
510 * @param string|null $filename
511 * @param string|null $mime_type
512 * @return array|WP_Error {
513 * Array on success or WP_Error if the file failed to save.
514 *
515 * @type string $path Path to the image file.
516 * @type string $file Name of the image file.
517 * @type int $width Image width.
518 * @type int $height Image height.
519 * @type string $mime-type The mime type of the image.
520 * @type int $filesize File size of the image.
521 * }
522 */
523 protected function _save( $image, $filename = null, $mime_type = null ) {
524 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
525
526 if ( ! $filename ) {
527 $filename = $this->generate_filename( null, null, $extension );
528 }
529
530 if ( function_exists( 'imageinterlace' ) ) {
531 /**
532 * Filters whether to output progressive images (if available).
533 *
534 * @since 6.5.0
535 *
536 * @param bool $interlace Whether to use progressive images for output if available. Default false.
537 * @param string $mime_type The mime type being saved.
538 */
539 imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) );
540 }
541
542 if ( 'image/gif' === $mime_type ) {
543 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
544 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
545 }
546 } elseif ( 'image/png' === $mime_type ) {
547 // Convert from full colors to index colors, like original PNG.
548 if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
549 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
550 }
551
552 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
553 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
554 }
555 } elseif ( 'image/jpeg' === $mime_type ) {
556 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
557 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
558 }
559 } elseif ( 'image/webp' === $mime_type ) {
560 if ( ! function_exists( 'imagewebp' )
561 || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) )
562 ) {
563 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
564 }
565 } elseif ( 'image/avif' === $mime_type ) {
566 if ( ! function_exists( 'imageavif' )
567 || ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) )
568 ) {
569 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
570 }
571 } else {
572 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
573 }
574
575 // Set correct file permissions.
576 $stat = stat( dirname( $filename ) );
577 $perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
578 chmod( $filename, $perms );
579
580 return array(
581 'path' => $filename,
582 /**
583 * Filters the name of the saved image file.
584 *
585 * @since 2.6.0
586 *
587 * @param string $filename Name of the file.
588 */
589 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
590 'width' => $this->size['width'],
591 'height' => $this->size['height'],
592 'mime-type' => $mime_type,
593 'filesize' => wp_filesize( $filename ),
594 );
595 }
596
597 /**
598 * Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
599 *
600 * @since 6.7.0
601 * @since 6.8.0 The `$dims` parameter was added.
602 *
603 * @param int $quality Compression Quality. Range: [1,100]
604 * @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
605 * @return true|WP_Error True if set successfully; WP_Error on failure.
606 */
607 public function set_quality( $quality = null, $dims = array() ) {
608 $quality_result = parent::set_quality( $quality, $dims );
609 if ( is_wp_error( $quality_result ) ) {
610 return $quality_result;
611 } else {
612 $quality = $this->get_quality();
613 }
614
615 // Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless.
616 try {
617 if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) {
618 $webp_info = wp_get_webp_info( $this->file );
619 if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
620 $quality = IMG_WEBP_LOSSLESS;
621 parent::set_quality( $quality, $dims );
622 }
623 }
624 } catch ( Exception $e ) {
625 return new WP_Error( 'image_quality_error', $e->getMessage() );
626 }
627 $this->quality = $quality;
628 return true;
629 }
630
631 /**
632 * Returns stream of current image.
633 *
634 * @since 3.5.0
635 *
636 * @param string $mime_type The mime type of the image.
637 * @return bool True on success, false on failure.
638 */
639 public function stream( $mime_type = null ) {
640 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
641
642 switch ( $mime_type ) {
643 case 'image/png':
644 header( 'Content-Type: image/png' );
645 return imagepng( $this->image );
646 case 'image/gif':
647 header( 'Content-Type: image/gif' );
648 return imagegif( $this->image );
649 case 'image/webp':
650 if ( function_exists( 'imagewebp' ) ) {
651 header( 'Content-Type: image/webp' );
652 return imagewebp( $this->image, null, $this->get_quality() );
653 } else {
654 // Fall back to JPEG.
655 header( 'Content-Type: image/jpeg' );
656 return imagejpeg( $this->image, null, $this->get_quality() );
657 }
658 case 'image/avif':
659 if ( function_exists( 'imageavif' ) ) {
660 header( 'Content-Type: image/avif' );
661 return imageavif( $this->image, null, $this->get_quality() );
662 }
663 // Fall back to JPEG.
664 default:
665 header( 'Content-Type: image/jpeg' );
666 return imagejpeg( $this->image, null, $this->get_quality() );
667 }
668 }
669
670 /**
671 * Either calls editor's save function or handles file as a stream.
672 *
673 * @since 3.5.0
674 *
675 * @param string $filename
676 * @param callable $callback
677 * @param array $arguments
678 * @return bool
679 */
680 protected function make_image( $filename, $callback, $arguments ) {
681 if ( wp_is_stream( $filename ) ) {
682 $arguments[1] = null;
683 }
684
685 return parent::make_image( $filename, $callback, $arguments );
686 }
687}
688