Make WordPress Core


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