Code Modernization: Introduce is_gd_image() to check for PHP 8 GdImage object instances.

In PHP 8, the GD extension uses `GdImage` objects instead of resources for its underlying data structures.

This updates the existing `is_resource()` calls for image resources in core to accomodate for `GdImage` instances as well.

Props ayeshrajans, jrf.
Fixes #50833.
Built from https://develop.svn.wordpress.org/trunk@48798


git-svn-id: http://core.svn.wordpress.org/trunk@48560 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov
2020-08-16 13:33:09 +00:00
parent a5edb2a8a1
commit 12c8f0e678
7 changed files with 95 additions and 51 deletions

View File

@@ -291,8 +291,8 @@ function wp_stream_image( $image, $mime_type, $attachment_id ) {
* @since 2.9.0
* @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
*
* @param resource $image Image resource to be streamed.
* @param int $attachment_id The attachment post ID.
* @param resource|GdImage $image Image resource to be streamed.
* @param int $attachment_id The attachment post ID.
*/
$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
@@ -420,19 +420,22 @@ function _image_get_preview_ratio( $w, $h ) {
* @see WP_Image_Editor::rotate()
*
* @ignore
* @param resource $img Image resource.
* @param float|int $angle Image rotation angle, in degrees.
* @return resource|false GD image resource, false otherwise.
* @param resource|GdImage $img Image resource.
* @param float|int $angle Image rotation angle, in degrees.
* @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
*/
function _rotate_image_resource( $img, $angle ) {
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
if ( function_exists( 'imagerotate' ) ) {
$rotated = imagerotate( $img, $angle, 0 );
if ( is_resource( $rotated ) ) {
if ( is_gd_image( $rotated ) ) {
imagedestroy( $img );
$img = $rotated;
}
}
return $img;
}
@@ -444,17 +447,19 @@ function _rotate_image_resource( $img, $angle ) {
* @see WP_Image_Editor::flip()
*
* @ignore
* @param resource $img Image resource.
* @param bool $horz Whether to flip horizontally.
* @param bool $vert Whether to flip vertically.
* @return resource (maybe) flipped image resource.
* @param resource|GdImage $img Image resource or GdImage instance.
* @param bool $horz Whether to flip horizontally.
* @param bool $vert Whether to flip vertically.
* @return resource|GdImage (maybe) flipped image resource or GdImage instance.
*/
function _flip_image_resource( $img, $horz, $vert ) {
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
$w = imagesx( $img );
$h = imagesy( $img );
$dst = wp_imagecreatetruecolor( $w, $h );
if ( is_resource( $dst ) ) {
if ( is_gd_image( $dst ) ) {
$sx = $vert ? ( $w - 1 ) : 0;
$sy = $horz ? ( $h - 1 ) : 0;
$sw = $vert ? -$w : $w;
@@ -465,6 +470,7 @@ function _flip_image_resource( $img, $horz, $vert ) {
$img = $dst;
}
}
return $img;
}
@@ -474,21 +480,23 @@ function _flip_image_resource( $img, $horz, $vert ) {
* @since 2.9.0
*
* @ignore
* @param resource $img Image resource.
* @param float $x Source point x-coordinate.
* @param float $y Source point y-coordinate.
* @param float $w Source width.
* @param float $h Source height.
* @return resource (maybe) cropped image resource.
* @param resource|GdImage $img Image resource or GdImage instance.
* @param float $x Source point x-coordinate.
* @param float $y Source point y-coordinate.
* @param float $w Source width.
* @param float $h Source height.
* @return resource|GdImage (maybe) cropped image resource or GdImage instance.
*/
function _crop_image_resource( $img, $x, $y, $w, $h ) {
$dst = wp_imagecreatetruecolor( $w, $h );
if ( is_resource( $dst ) ) {
if ( is_gd_image( $dst ) ) {
if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
imagedestroy( $img );
$img = $dst;
}
}
return $img;
}
@@ -502,7 +510,7 @@ function _crop_image_resource( $img, $x, $y, $w, $h ) {
* @return WP_Image_Editor WP_Image_Editor instance with changes applied.
*/
function image_edit_apply_changes( $image, $changes ) {
if ( is_resource( $image ) ) {
if ( is_gd_image( $image ) ) {
/* translators: 1: $image, 2: WP_Image_Editor */
_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
}
@@ -566,7 +574,7 @@ function image_edit_apply_changes( $image, $changes ) {
* @param array $changes Array of change operations.
*/
$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
} elseif ( is_resource( $image ) ) {
} elseif ( is_gd_image( $image ) ) {
/**
* Filters the GD image resource before applying changes to the image.
@@ -574,8 +582,8 @@ function image_edit_apply_changes( $image, $changes ) {
* @since 2.9.0
* @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
*
* @param resource $image GD image resource.
* @param array $changes Array of change operations.
* @param resource|GdImage $image GD image resource or GdImage instance.
* @param array $changes Array of change operations.
*/
$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
}