Make WordPress Core


Ignore:
Timestamp:
08/16/2020 01:31:57 PM (4 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-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' );
Note: See TracChangeset for help on using the changeset viewer.