WordPress.org

Make WordPress Core

Changeset 48798


Ignore:
Timestamp:
08/16/2020 01:31:57 PM (2 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r48375 r48798  
    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' );
     
    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 or GdImage instance, false otherwise.
    426426 */
    427427function _rotate_image_resource( $img, $angle ) {
    428428    _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
     429
    429430    if ( function_exists( 'imagerotate' ) ) {
    430431        $rotated = imagerotate( $img, $angle, 0 );
    431         if ( is_resource( $rotated ) ) {
     432
     433        if ( is_gd_image( $rotated ) ) {
    432434            imagedestroy( $img );
    433435            $img = $rotated;
    434436        }
    435437    }
     438
    436439    return $img;
    437440}
     
    445448 *
    446449 * @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.
     450 * @param resource|GdImage $img  Image resource or GdImage instance.
     451 * @param bool             $horz Whether to flip horizontally.
     452 * @param bool             $vert Whether to flip vertically.
     453 * @return resource|GdImage (maybe) flipped image resource or GdImage instance.
    451454 */
    452455function _flip_image_resource( $img, $horz, $vert ) {
    453456    _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
     457
    454458    $w   = imagesx( $img );
    455459    $h   = imagesy( $img );
    456460    $dst = wp_imagecreatetruecolor( $w, $h );
    457     if ( is_resource( $dst ) ) {
     461
     462    if ( is_gd_image( $dst ) ) {
    458463        $sx = $vert ? ( $w - 1 ) : 0;
    459464        $sy = $horz ? ( $h - 1 ) : 0;
     
    466471        }
    467472    }
     473
    468474    return $img;
    469475}
     
    475481 *
    476482 * @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.
     483 * @param resource|GdImage $img Image resource or GdImage instance.
     484 * @param float            $x   Source point x-coordinate.
     485 * @param float            $y   Source point y-coordinate.
     486 * @param float            $w   Source width.
     487 * @param float            $h   Source height.
     488 * @return resource|GdImage (maybe) cropped image resource or GdImage instance.
    483489 */
    484490function _crop_image_resource( $img, $x, $y, $w, $h ) {
    485491    $dst = wp_imagecreatetruecolor( $w, $h );
    486     if ( is_resource( $dst ) ) {
     492
     493    if ( is_gd_image( $dst ) ) {
    487494        if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
    488495            imagedestroy( $img );
     
    490497        }
    491498    }
     499
    492500    return $img;
    493501}
     
    503511 */
    504512function image_edit_apply_changes( $image, $changes ) {
    505     if ( is_resource( $image ) ) {
     513    if ( is_gd_image( $image ) ) {
    506514        /* translators: 1: $image, 2: WP_Image_Editor */
    507515        _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
     
    567575         */
    568576        $image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
    569     } elseif ( is_resource( $image ) ) {
     577    } elseif ( is_gd_image( $image ) ) {
    570578
    571579        /**
     
    575583         * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
    576584         *
    577          * @param resource $image   GD image resource.
    578          * @param array    $changes Array of change operations.
     585         * @param resource|GdImage $image   GD image resource or GdImage instance.
     586         * @param array            $changes Array of change operations.
    579587         */
    580588        $image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
  • trunk/src/wp-admin/includes/image.php

    r48586 r48798  
    914914
    915915/**
     916 * Determines whether the value is an acceptable type for GD image functions.
     917 *
     918 * In PHP 8.0, the GD extension uses GdImage objects for its data structures.
     919 * This function checks if the passed value is either a resource of type `gd`
     920 * or a GdImage object instance. Any other type will return false.
     921 *
     922 * @since 5.6.0
     923 *
     924 * @param resource|GdImage|false $image A value to check for the type for.
     925 * @return bool True if $image is either a GD image resource or GdImage instance,
     926 *              false otherwise.
     927 */
     928function is_gd_image( $image ) {
     929    if ( is_resource( $image ) && 'gd' === get_resource_type( $image )
     930        || is_object( $image ) && $image instanceof GdImage
     931    ) {
     932        return true;
     933    }
     934
     935    return false;
     936}
     937
     938/**
    916939 * Load an image resource for editing.
    917940 *
     
    919942 *
    920943 * @param string $attachment_id Attachment ID.
    921  * @param string $mime_type Image mime type.
    922  * @param string $size Optional. Image size, defaults to 'full'.
    923  * @return resource|false The resulting image resource on success, false on failure.
     944 * @param string $mime_type     Image mime type.
     945 * @param string $size          Optional. Image size. Default 'full'.
     946 * @return resource|GdImage|false The resulting image resource or GdImage instance on success,
     947 *                                false on failure.
    924948 */
    925949function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
     
    943967            break;
    944968    }
    945     if ( is_resource( $image ) ) {
     969
     970    if ( is_gd_image( $image ) ) {
    946971        /**
    947972         * Filters the current image being loaded for editing.
     
    949974         * @since 2.9.0
    950975         *
    951          * @param resource $image         Current image.
    952          * @param string   $attachment_id Attachment ID.
    953          * @param string   $size          Image size.
     976         * @param resource|GdImage $image         Current image.
     977         * @param string           $attachment_id Attachment ID.
     978         * @param string           $size          Image size.
    954979         */
    955980        $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
     981
    956982        if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
    957983            imagealphablending( $image, false );
     
    959985        }
    960986    }
     987
    961988    return $image;
    962989}
     
    972999 *
    9731000 * @param string $attachment_id Attachment ID.
    974  * @param string $size Optional. Image size, defaults to 'full'.
     1001 * @param string $size          Optional. Image size. Default 'full'.
    9751002 * @return string|false File path or url on success, false on failure.
    9761003 */
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r48586 r48798  
    1818     * GD Resource.
    1919     *
    20      * @var resource
     20     * @var resource|GdImage
    2121     */
    2222    protected $image;
     
    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        }
     
    139139    /**
    140140     * Resizes current image.
    141      * Wraps _resize, since _resize returns a GD Resource.
    142      *
    143      * At minimum, either a height or width must be provided.
    144      * If one of the two is set to null, the resize will
    145      * maintain aspect ratio according to the provided dimension.
     141     *
     142     * Wraps `::_resize()` which returns a GD resource or GdImage instance.
     143     *
     144     * At minimum, either a height or width must be provided. If one of the two is set
     145     * to null, the resize will maintain aspect ratio according to the provided dimension.
    146146     *
    147147     * @since 3.5.0
     
    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;
     
    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 ) {
     
    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;
     
    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;
     
    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 );
     
    363363            }
    364364        }
     365
    365366        return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file );
    366367    }
     
    380381        $dst = wp_imagecreatetruecolor( $w, $h );
    381382
    382         if ( is_resource( $dst ) ) {
     383        if ( is_gd_image( $dst ) ) {
    383384            $sx = $vert ? ( $w - 1 ) : 0;
    384385            $sy = $horz ? ( $h - 1 ) : 0;
     
    392393            }
    393394        }
     395
    394396        return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
    395397    }
     
    416418
    417419    /**
    418      * @param resource    $image
    419      * @param string|null $filename
    420      * @param string|null $mime_type
     420     * @param resource|GdImage $image
     421     * @param string|null      $filename
     422     * @param string|null      $mime_type
    421423     * @return array|WP_Error
    422424     */
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r48586 r48798  
    549549            return new WP_Error( 'image_crop_error', $e->getMessage() );
    550550        }
     551
    551552        return $this->update_size();
    552553    }
     
    583584            return new WP_Error( 'image_rotate_error', $e->getMessage() );
    584585        }
     586
    585587        return true;
    586588    }
  • trunk/src/wp-includes/deprecated.php

    r48695 r48798  
    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|string The resulting image resource or GdImage instance on success,
     3200 *                                 error string on failure.
    32003201 */
    32013202function wp_load_image( $file ) {
     
    32183219    $image = imagecreatefromstring( file_get_contents( $file ) );
    32193220
    3220     if ( ! is_resource( $image ) ) {
     3221    if ( ! is_gd_image( $image ) ) {
    32213222        /* translators: %s: File name. */
    32223223        return sprintf( __( 'File “%s” is not an image.' ), $file );
  • trunk/src/wp-includes/media.php

    r48782 r48798  
    34603460 *
    34613461 * @param int $width  Image width in pixels.
    3462  * @param int $height Image height in pixels..
    3463  * @return resource The GD image resource.
     3462 * @param int $height Image height in pixels.
     3463 * @return resource|GdImage The GD image resource or 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
     3468    if ( is_gd_image( $img )
     3469        && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' )
     3470    ) {
    34683471        imagealphablending( $img, false );
    34693472        imagesavealpha( $img, true );
    34703473    }
     3474
    34713475    return $img;
    34723476}
  • trunk/tests/phpunit/tests/image/functions.php

    r48464 r48798  
    124124    }
    125125
     126
     127    /**
     128     * @ticket 50833
     129     */
     130    function test_is_gd_image_invalid_types() {
     131        $this->assertFalse( is_gd_image( new stdClass() ) );
     132        $this->assertFalse( is_gd_image( array() ) );
     133        $this->assertFalse( is_gd_image( null ) );
     134
     135        $handle = fopen( __FILE__, 'r' );
     136        $this->assertFalse( is_gd_image( $handle ) );
     137        fclose( $handle );
     138    }
     139
     140    /**
     141     * @ticket 50833
     142     * @requires extension gd
     143     */
     144    function test_is_gd_image_valid_types() {
     145        $this->assertTrue( is_gd_image( imagecreate( 5, 5 ) ) );
     146    }
     147
    126148    /**
    127149     * Test save image file and mime_types
Note: See TracChangeset for help on using the changeset viewer.