diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php
index 1363f00ae7..c5673cc138 100644
|
a
|
b
|
function wp_stream_image( $image, $mime_type, $attachment_id ) { |
| 291 | 291 | * @since 2.9.0 |
| 292 | 292 | * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead. |
| 293 | 293 | * |
| 294 | | * @param resource $image Image resource to be streamed. |
| 295 | | * @param int $attachment_id The attachment post ID. |
| | 294 | * @param resource|GdImage $image Image resource to be streamed. |
| | 295 | * @param int $attachment_id The attachment post ID. |
| 296 | 296 | */ |
| 297 | 297 | $image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' ); |
| 298 | 298 | |
| … |
… |
function _image_get_preview_ratio( $w, $h ) { |
| 420 | 420 | * @see WP_Image_Editor::rotate() |
| 421 | 421 | * |
| 422 | 422 | * @ignore |
| 423 | | * @param resource $img Image resource. |
| 424 | | * @param float|int $angle Image rotation angle, in degrees. |
| 425 | | * @return resource|false GD image resource, false otherwise. |
| | 423 | * @param resource|GdImage $img Image resource. |
| | 424 | * @param float|int $angle Image rotation angle, in degrees. |
| | 425 | * @return resource|GdImage|false GD image resource/GdImage instance, false otherwise. |
| 426 | 426 | */ |
| 427 | 427 | function _rotate_image_resource( $img, $angle ) { |
| 428 | 428 | _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' ); |
| 429 | 429 | if ( function_exists( 'imagerotate' ) ) { |
| 430 | 430 | $rotated = imagerotate( $img, $angle, 0 ); |
| 431 | | if ( is_resource( $rotated ) ) { |
| | 431 | if ( is_gd_image( $rotated ) ) { |
| 432 | 432 | imagedestroy( $img ); |
| 433 | 433 | $img = $rotated; |
| 434 | 434 | } |
| … |
… |
function _rotate_image_resource( $img, $angle ) { |
| 444 | 444 | * @see WP_Image_Editor::flip() |
| 445 | 445 | * |
| 446 | 446 | * @ignore |
| 447 | | * @param resource $img Image resource. |
| 448 | | * @param bool $horz Whether to flip horizontally. |
| 449 | | * @param bool $vert Whether to flip vertically. |
| 450 | | * @return resource (maybe) flipped image resource. |
| | 447 | * @param resource|GdImage $img Image resource/GdImage instance. |
| | 448 | * @param bool $horz Whether to flip horizontally. |
| | 449 | * @param bool $vert Whether to flip vertically. |
| | 450 | * @return resource|GdImage (maybe) flipped image resource/GdImage instance. |
| 451 | 451 | */ |
| 452 | 452 | function _flip_image_resource( $img, $horz, $vert ) { |
| 453 | 453 | _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' ); |
| 454 | 454 | $w = imagesx( $img ); |
| 455 | 455 | $h = imagesy( $img ); |
| 456 | 456 | $dst = wp_imagecreatetruecolor( $w, $h ); |
| 457 | | if ( is_resource( $dst ) ) { |
| | 457 | if ( is_gd_image( $dst ) ) { |
| 458 | 458 | $sx = $vert ? ( $w - 1 ) : 0; |
| 459 | 459 | $sy = $horz ? ( $h - 1 ) : 0; |
| 460 | 460 | $sw = $vert ? -$w : $w; |
| … |
… |
function _flip_image_resource( $img, $horz, $vert ) { |
| 474 | 474 | * @since 2.9.0 |
| 475 | 475 | * |
| 476 | 476 | * @ignore |
| 477 | | * @param resource $img Image resource. |
| 478 | | * @param float $x Source point x-coordinate. |
| 479 | | * @param float $y Source point y-coordinate. |
| 480 | | * @param float $w Source width. |
| 481 | | * @param float $h Source height. |
| 482 | | * @return resource (maybe) cropped image resource. |
| | 477 | * @param resource|GdImage $img Image resource/GdImage instance. |
| | 478 | * @param float $x Source point x-coordinate. |
| | 479 | * @param float $y Source point y-coordinate. |
| | 480 | * @param float $w Source width. |
| | 481 | * @param float $h Source height. |
| | 482 | * @return resource|GdImage (maybe) cropped image resource/GdImage instance. |
| 483 | 483 | */ |
| 484 | 484 | function _crop_image_resource( $img, $x, $y, $w, $h ) { |
| 485 | 485 | $dst = wp_imagecreatetruecolor( $w, $h ); |
| 486 | | if ( is_resource( $dst ) ) { |
| | 486 | if ( is_gd_image( $dst ) ) { |
| 487 | 487 | if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) { |
| 488 | 488 | imagedestroy( $img ); |
| 489 | 489 | $img = $dst; |
| … |
… |
function image_edit_apply_changes( $image, $changes ) { |
| 566 | 566 | * @param array $changes Array of change operations. |
| 567 | 567 | */ |
| 568 | 568 | $image = apply_filters( 'wp_image_editor_before_change', $image, $changes ); |
| 569 | | } elseif ( is_resource( $image ) ) { |
| | 569 | } elseif ( is_gd_image( $image ) ) { |
| 570 | 570 | |
| 571 | 571 | /** |
| 572 | 572 | * Filters the GD image resource before applying changes to the image. |
| … |
… |
function image_edit_apply_changes( $image, $changes ) { |
| 574 | 574 | * @since 2.9.0 |
| 575 | 575 | * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead. |
| 576 | 576 | * |
| 577 | | * @param resource $image GD image resource. |
| 578 | | * @param array $changes Array of change operations. |
| | 577 | * @param resource|GdImage $image GD image resource/GdImage instance. |
| | 578 | * @param array $changes Array of change operations. |
| 579 | 579 | */ |
| 580 | 580 | $image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' ); |
| 581 | 581 | } |
diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
index f0a3d38bb4..f6281a65d9 100644
|
a
|
b
|
function file_is_displayable_image( $path ) { |
| 920 | 920 | * @param string $attachment_id Attachment ID. |
| 921 | 921 | * @param string $mime_type Image mime type. |
| 922 | 922 | * @param string $size Optional. Image size, defaults to 'full'. |
| 923 | | * @return resource|false The resulting image resource on success, false on failure. |
| | 923 | * @return resource|GdImage|false The resulting image resource/GdImage instance on success, false on failure. |
| 924 | 924 | */ |
| 925 | 925 | function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
| 926 | 926 | $filepath = _load_image_to_edit_path( $attachment_id, $size ); |
| … |
… |
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
| 942 | 942 | $image = false; |
| 943 | 943 | break; |
| 944 | 944 | } |
| 945 | | if ( is_resource( $image ) ) { |
| | 945 | if ( is_gd_image( $image ) ) { |
| 946 | 946 | /** |
| 947 | 947 | * Filters the current image being loaded for editing. |
| 948 | 948 | * |
| 949 | 949 | * @since 2.9.0 |
| 950 | 950 | * |
| 951 | | * @param resource $image Current image. |
| 952 | | * @param string $attachment_id Attachment ID. |
| 953 | | * @param string $size Image size. |
| | 951 | * @param resource|GdImage $image Current image. |
| | 952 | * @param string $attachment_id Attachment ID. |
| | 953 | * @param string $size Image size. |
| 954 | 954 | */ |
| 955 | 955 | $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); |
| 956 | 956 | if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
| … |
… |
function _copy_image_file( $attachment_id ) { |
| 1061 | 1061 | |
| 1062 | 1062 | return $dst_file; |
| 1063 | 1063 | } |
| | 1064 | |
| | 1065 | /** |
| | 1066 | * Returns whether the provided argument is of a resource, or a PHP 8 GdImage object. |
| | 1067 | * |
| | 1068 | * @ticket 50833 |
| | 1069 | * @see https://php.watch/versions/8.0/gdimage#gdimage-is-resource |
| | 1070 | * @since 5.6 |
| | 1071 | * |
| | 1072 | * @param $image |
| | 1073 | * |
| | 1074 | * @return bool |
| | 1075 | */ |
| | 1076 | function is_gd_image( $image ) { |
| | 1077 | return is_resource( $image ) || ( is_object( $image ) && $image instanceof GdImage ); |
| | 1078 | } |
diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
index 3ead252dfe..4d0e3ebda5 100644
|
a
|
b
|
class WP_Image_Editor_GD extends WP_Image_Editor { |
| 17 | 17 | /** |
| 18 | 18 | * GD Resource. |
| 19 | 19 | * |
| 20 | | * @var resource |
| | 20 | * @var resource|GdImage |
| 21 | 21 | */ |
| 22 | 22 | protected $image; |
| 23 | 23 | |
| … |
… |
public function load() { |
| 95 | 95 | |
| 96 | 96 | $this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); |
| 97 | 97 | |
| 98 | | if ( ! is_resource( $this->image ) ) { |
| | 98 | if ( ! is_gd_image( $this->image ) ) { |
| 99 | 99 | return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); |
| 100 | 100 | } |
| 101 | 101 | |
| … |
… |
protected function update_size( $width = false, $height = false ) { |
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | 140 | * Resizes current image. |
| 141 | | * Wraps _resize, since _resize returns a GD Resource. |
| | 141 | * Wraps _resize, since _resize returns a GD Resource/GdImage instance. |
| 142 | 142 | * |
| 143 | 143 | * At minimum, either a height or width must be provided. |
| 144 | 144 | * If one of the two is set to null, the resize will |
| … |
… |
public function resize( $max_w, $max_h, $crop = false ) { |
| 158 | 158 | |
| 159 | 159 | $resized = $this->_resize( $max_w, $max_h, $crop ); |
| 160 | 160 | |
| 161 | | if ( is_resource( $resized ) ) { |
| | 161 | if ( is_gd_image( $resized ) ) { |
| 162 | 162 | imagedestroy( $this->image ); |
| 163 | 163 | $this->image = $resized; |
| 164 | 164 | return true; |
| … |
… |
public function resize( $max_w, $max_h, $crop = false ) { |
| 174 | 174 | * @param int $max_w |
| 175 | 175 | * @param int $max_h |
| 176 | 176 | * @param bool|array $crop |
| 177 | | * @return resource|WP_Error |
| | 177 | * @return resource|GdImage|WP_Error |
| 178 | 178 | */ |
| 179 | 179 | protected function _resize( $max_w, $max_h, $crop = false ) { |
| 180 | 180 | $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
| … |
… |
protected function _resize( $max_w, $max_h, $crop = false ) { |
| 188 | 188 | $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
| 189 | 189 | imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
| 190 | 190 | |
| 191 | | if ( is_resource( $resized ) ) { |
| | 191 | if ( is_gd_image( $resized ) ) { |
| 192 | 192 | $this->update_size( $dst_w, $dst_h ); |
| 193 | 193 | return $resized; |
| 194 | 194 | } |
| … |
… |
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu |
| 329 | 329 | |
| 330 | 330 | imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
| 331 | 331 | |
| 332 | | if ( is_resource( $dst ) ) { |
| | 332 | if ( is_gd_image( $dst ) ) { |
| 333 | 333 | imagedestroy( $this->image ); |
| 334 | 334 | $this->image = $dst; |
| 335 | 335 | $this->update_size(); |
| … |
… |
public function rotate( $angle ) { |
| 353 | 353 | $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); |
| 354 | 354 | $rotated = imagerotate( $this->image, $angle, $transparency ); |
| 355 | 355 | |
| 356 | | if ( is_resource( $rotated ) ) { |
| | 356 | if ( is_gd_image( $rotated ) ) { |
| 357 | 357 | imagealphablending( $rotated, true ); |
| 358 | 358 | imagesavealpha( $rotated, true ); |
| 359 | 359 | imagedestroy( $this->image ); |
| … |
… |
public function flip( $horz, $vert ) { |
| 379 | 379 | $h = $this->size['height']; |
| 380 | 380 | $dst = wp_imagecreatetruecolor( $w, $h ); |
| 381 | 381 | |
| 382 | | if ( is_resource( $dst ) ) { |
| | 382 | if ( is_gd_image( $dst ) ) { |
| 383 | 383 | $sx = $vert ? ( $w - 1 ) : 0; |
| 384 | 384 | $sy = $horz ? ( $h - 1 ) : 0; |
| 385 | 385 | $sw = $vert ? -$w : $w; |
| … |
… |
public function save( $filename = null, $mime_type = null ) { |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | /** |
| 418 | | * @param resource $image |
| 419 | | * @param string|null $filename |
| 420 | | * @param string|null $mime_type |
| | 418 | * @param resource|GdImage $image |
| | 419 | * @param string|null $filename |
| | 420 | * @param string|null $mime_type |
| 421 | 421 | * @return array|WP_Error |
| 422 | 422 | */ |
| 423 | 423 | protected function _save( $image, $filename = null, $mime_type = null ) { |
diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php
index a586c336c7..a748ad3302 100644
|
a
|
b
|
function _get_post_ancestors( &$post ) { |
| 3196 | 3196 | * @see wp_get_image_editor() |
| 3197 | 3197 | * |
| 3198 | 3198 | * @param string $file Filename of the image to load. |
| 3199 | | * @return resource The resulting image resource on success, Error string on failure. |
| | 3199 | * @return resource|GdImage The resulting image resource on success, Error string on failure. |
| 3200 | 3200 | */ |
| 3201 | 3201 | function wp_load_image( $file ) { |
| 3202 | 3202 | _deprecated_function( __FUNCTION__, '3.5.0', 'wp_get_image_editor()' ); |
| … |
… |
function wp_load_image( $file ) { |
| 3217 | 3217 | |
| 3218 | 3218 | $image = imagecreatefromstring( file_get_contents( $file ) ); |
| 3219 | 3219 | |
| 3220 | | if ( ! is_resource( $image ) ) { |
| | 3220 | if ( ! is_gd_image( $image ) ) { |
| 3221 | 3221 | /* translators: %s: File name. */ |
| 3222 | 3222 | return sprintf( __( 'File “%s” is not an image.' ), $file ); |
| 3223 | 3223 | } |
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
index 801e918f3f..c8ce4e2e76 100644
|
a
|
b
|
function get_taxonomies_for_attachments( $output = 'names' ) { |
| 3461 | 3461 | * |
| 3462 | 3462 | * @param int $width Image width in pixels. |
| 3463 | 3463 | * @param int $height Image height in pixels.. |
| 3464 | | * @return resource The GD image resource. |
| | 3464 | * @return resource|GdImage The GD image resource/GdImage instance. |
| 3465 | 3465 | */ |
| 3466 | 3466 | function wp_imagecreatetruecolor( $width, $height ) { |
| 3467 | 3467 | $img = imagecreatetruecolor( $width, $height ); |
| 3468 | | if ( is_resource( $img ) && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
| | 3468 | if ( is_gd_image( $img ) && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
| 3469 | 3469 | imagealphablending( $img, false ); |
| 3470 | 3470 | imagesavealpha( $img, true ); |
| 3471 | 3471 | } |
diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php
index cfffd71922..eb40cfc123 100644
|
a
|
b
|
public function test_pdf_preview_doesnt_overwrite_existing_jpeg() { |
| 665 | 665 | unlink( $temp_dir . $size['file'] ); |
| 666 | 666 | } |
| 667 | 667 | } |
| | 668 | |
| | 669 | /** |
| | 670 | * @ticket 50833 |
| | 671 | */ |
| | 672 | function test_is_gd_image() { |
| | 673 | if ( ! extension_loaded( 'gd' ) ) { |
| | 674 | $this->markTestSkipped( 'The GD PHP extension is not loaded.' ); |
| | 675 | } |
| | 676 | |
| | 677 | $this->assertTrue( is_gd_image( imagecreate( 5, 5 ) ) ); |
| | 678 | $this->assertFalse( is_gd_image( new stdClass() ) ); |
| | 679 | $this->assertFalse( is_gd_image( array() ) ); |
| | 680 | $this->assertFalse( is_gd_image( null ) ); |
| | 681 | } |
| 668 | 682 | } |