Make WordPress Core


Ignore:
Timestamp:
08/16/2020 01:31:57 PM (6 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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         */
Note: See TracChangeset for help on using the changeset viewer.