WordPress.org

Make WordPress Core

Ticket #50833: 50833-2.patch

File 50833-2.patch, 13.2 KB (added by ayeshrajans, 3 months ago)

Tests: https://travis-ci.com/github/Ayesh/wordpress-develop/builds/179560442 , Interdiff: https://github.com/Ayesh/wordpress-develop/commit/c8ceabe6030c75842dba78c8118045f3eba19982

  • src/wp-admin/includes/image-edit.php

    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 ) { 
    291291                 * @since 2.9.0
    292292                 * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
    293293                 *
    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.
    296296                 */
    297297                $image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
    298298
    function _image_get_preview_ratio( $w, $h ) { 
    420420 * @see WP_Image_Editor::rotate()
    421421 *
    422422 * @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.
    426426 */
    427427function _rotate_image_resource( $img, $angle ) {
    428428        _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
    429429        if ( function_exists( 'imagerotate' ) ) {
    430430                $rotated = imagerotate( $img, $angle, 0 );
    431                 if ( is_resource( $rotated ) ) {
     431                if ( is_gd_image( $rotated ) ) {
    432432                        imagedestroy( $img );
    433433                        $img = $rotated;
    434434                }
    function _rotate_image_resource( $img, $angle ) { 
    444444 * @see WP_Image_Editor::flip()
    445445 *
    446446 * @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.
    451451 */
    452452function _flip_image_resource( $img, $horz, $vert ) {
    453453        _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
    454454        $w   = imagesx( $img );
    455455        $h   = imagesy( $img );
    456456        $dst = wp_imagecreatetruecolor( $w, $h );
    457         if ( is_resource( $dst ) ) {
     457        if ( is_gd_image( $dst ) ) {
    458458                $sx = $vert ? ( $w - 1 ) : 0;
    459459                $sy = $horz ? ( $h - 1 ) : 0;
    460460                $sw = $vert ? -$w : $w;
    function _flip_image_resource( $img, $horz, $vert ) { 
    474474 * @since 2.9.0
    475475 *
    476476 * @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.
    483483 */
    484484function _crop_image_resource( $img, $x, $y, $w, $h ) {
    485485        $dst = wp_imagecreatetruecolor( $w, $h );
    486         if ( is_resource( $dst ) ) {
     486        if ( is_gd_image( $dst ) ) {
    487487                if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
    488488                        imagedestroy( $img );
    489489                        $img = $dst;
    function image_edit_apply_changes( $image, $changes ) { 
    566566                 * @param array           $changes Array of change operations.
    567567                 */
    568568                $image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
    569         } elseif ( is_resource( $image ) ) {
     569        } elseif ( is_gd_image( $image ) ) {
    570570
    571571                /**
    572572                 * Filters the GD image resource before applying changes to the image.
    function image_edit_apply_changes( $image, $changes ) { 
    574574                 * @since 2.9.0
    575575                 * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
    576576                 *
    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.
    579579                 */
    580580                $image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
    581581        }
  • src/wp-admin/includes/image.php

    diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
    index f0a3d38bb4..e5338b6860 100644
    a b function file_is_displayable_image( $path ) { 
    920920 * @param string $attachment_id Attachment ID.
    921921 * @param string $mime_type Image mime type.
    922922 * @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.
    924924 */
    925925function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
    926926        $filepath = _load_image_to_edit_path( $attachment_id, $size );
    function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 
    942942                        $image = false;
    943943                        break;
    944944        }
    945         if ( is_resource( $image ) ) {
     945        if ( is_gd_image( $image ) ) {
    946946                /**
    947947                 * Filters the current image being loaded for editing.
    948948                 *
    949949                 * @since 2.9.0
    950950                 *
    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.
    954954                 */
    955955                $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
    956956                if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
    function _copy_image_file( $attachment_id ) { 
    10611061
    10621062        return $dst_file;
    10631063}
     1064
     1065/**
     1066 * A helper function to determine if the passed value is an acceptable type
     1067 * for GD extension functions.
     1068 * In PHP 8.0, GD extension uses GdImage objects as its primitive data type.
     1069 * This function checks if the passed value is either a resource of type
     1070 * `gd`, or a `\GdImage` object. All other types will return false.
     1071 *
     1072 * @see https://php.watch/versions/8.0/gdimage#gdimage-is-resource
     1073 * @since 5.6.0
     1074 *
     1075 * @param mixed $image A value to check for `gd` resource or `\GdImage` object.
     1076 *
     1077 * @return bool True if $image is either a resource of type `gd`, or `\GdImage`
     1078 *  object. False otherwise.
     1079 */
     1080function is_gd_image( $image ) {
     1081        return ( is_resource( $image ) && get_resource_type( $image ) === 'gd' ) || ( is_object( $image ) && $image instanceof GdImage );
     1082}
  • src/wp-includes/class-wp-image-editor-gd.php

    diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
    index 3ead252dfe..fcfefaac33 100644
    a b class WP_Image_Editor_GD extends WP_Image_Editor { 
    1717        /**
    1818         * GD Resource.
    1919         *
    20          * @var resource
     20         * @var resource|GdImage
    2121         */
    2222        protected $image;
    2323
    public function load() { 
    9595
    9696                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    9797
    98                 if ( ! is_resource( $this->image ) ) {
     98                if ( ! is_gd_image( $this->image ) ) {
    9999                        return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
    100100                }
    101101
    protected function update_size( $width = false, $height = false ) { 
    138138
    139139        /**
    140140         * Resizes current image.
    141          * Wraps _resize, since _resize returns a GD Resource.
     141         * Wraps _resize, since _resize returns a GD Resource/GdImage instance.
    142142         *
    143143         * At minimum, either a height or width must be provided.
    144144         * If one of the two is set to null, the resize will
    public function resize( $max_w, $max_h, $crop = false ) { 
    158158
    159159                $resized = $this->_resize( $max_w, $max_h, $crop );
    160160
    161                 if ( is_resource( $resized ) ) {
     161                if ( is_gd_image( $resized ) ) {
    162162                        imagedestroy( $this->image );
    163163                        $this->image = $resized;
    164164                        return true;
    public function resize( $max_w, $max_h, $crop = false ) { 
    174174         * @param int        $max_w
    175175         * @param int        $max_h
    176176         * @param bool|array $crop
    177          * @return resource|WP_Error
     177         * @return resource|GdImage|WP_Error
    178178         */
    179179        protected function _resize( $max_w, $max_h, $crop = false ) {
    180180                $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
    protected function _resize( $max_w, $max_h, $crop = false ) { 
    188188                $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
    189189                imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    190190
    191                 if ( is_resource( $resized ) ) {
     191                if ( is_gd_image( $resized ) ) {
    192192                        $this->update_size( $dst_w, $dst_h );
    193193                        return $resized;
    194194                }
    public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu 
    329329
    330330                imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    331331
    332                 if ( is_resource( $dst ) ) {
     332                if ( is_gd_image( $dst ) ) {
    333333                        imagedestroy( $this->image );
    334334                        $this->image = $dst;
    335335                        $this->update_size();
    public function rotate( $angle ) { 
    353353                        $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
    354354                        $rotated      = imagerotate( $this->image, $angle, $transparency );
    355355
    356                         if ( is_resource( $rotated ) ) {
     356                        if ( is_gd_image( $rotated ) ) {
    357357                                imagealphablending( $rotated, true );
    358358                                imagesavealpha( $rotated, true );
    359359                                imagedestroy( $this->image );
    public function flip( $horz, $vert ) { 
    379379                $h   = $this->size['height'];
    380380                $dst = wp_imagecreatetruecolor( $w, $h );
    381381
    382                 if ( is_resource( $dst ) ) {
     382                if ( is_gd_image( $dst ) ) {
    383383                        $sx = $vert ? ( $w - 1 ) : 0;
    384384                        $sy = $horz ? ( $h - 1 ) : 0;
    385385                        $sw = $vert ? -$w : $w;
    public function save( $filename = null, $mime_type = null ) { 
    415415        }
    416416
    417417        /**
    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
    421421         * @return array|WP_Error
    422422         */
    423423        protected function _save( $image, $filename = null, $mime_type = null ) {
  • src/wp-includes/deprecated.php

    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 ) { 
    31963196 * @see wp_get_image_editor()
    31973197 *
    31983198 * @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.
    32003200 */
    32013201function wp_load_image( $file ) {
    32023202        _deprecated_function( __FUNCTION__, '3.5.0', 'wp_get_image_editor()' );
    function wp_load_image( $file ) { 
    32173217
    32183218        $image = imagecreatefromstring( file_get_contents( $file ) );
    32193219
    3220         if ( ! is_resource( $image ) ) {
     3220        if ( ! is_gd_image( $image ) ) {
    32213221                /* translators: %s: File name. */
    32223222                return sprintf( __( 'File “%s” is not an image.' ), $file );
    32233223        }
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 3df41f58f7..f89f02791d 100644
    a b function get_taxonomies_for_attachments( $output = 'names' ) { 
    34603460 *
    34613461 * @param int $width  Image width in pixels.
    34623462 * @param int $height Image height in pixels..
    3463  * @return resource The GD image resource.
     3463 * @return resource|GdImage The GD image resource/GdImage instance.
    34643464 */
    34653465function wp_imagecreatetruecolor( $width, $height ) {
    34663466        $img = imagecreatetruecolor( $width, $height );
    3467         if ( is_resource( $img ) && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
     3467        if ( is_gd_image( $img ) && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
    34683468                imagealphablending( $img, false );
    34693469                imagesavealpha( $img, true );
    34703470        }
  • tests/phpunit/tests/image/functions.php

    diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php
    index cfffd71922..d75ba0142d 100644
    a b public function test_pdf_preview_doesnt_overwrite_existing_jpeg() { 
    665665                        unlink( $temp_dir . $size['file'] );
    666666                }
    667667        }
     668
     669        /**
     670         * @ticket 50833
     671         * @see \Tests_Image_Functions::test_is_gd_image_valid_types
     672         */
     673        function test_is_gd_image_invalid_types() {
     674                $this->assertFalse( is_gd_image( new stdClass() ) );
     675                $this->assertFalse( is_gd_image( array() ) );
     676                $this->assertFalse( is_gd_image( null ) );
     677
     678                $handle = fopen( __FILE__, 'r' );
     679                $this->assertFalse( is_gd_image( $handle ) );
     680                fclose( $handle );
     681        }
     682
     683        /**
     684         * @ticket 50833
     685         * @requires extension gd
     686         * @see \Tests_Image_Functions::test_is_gd_image_invalid_types
     687         */
     688        function test_is_gd_image_valid_types() {
     689                $this->assertTrue( is_gd_image( imagecreate( 5, 5 ) ) );
     690        }
    668691}