Make WordPress Core

Changeset 50815


Ignore:
Timestamp:
05/05/2021 06:46:10 PM (9 months ago)
Author:
SergeyBiryukov
Message:

Media: Move retrieving WebP image size information into wp_getimagesize().

Remove _wp_get_image_size().

Follow-up to [50146], [50810], [50814].

Props johnjamesjacoby.
See #35725.

Location:
trunk/src
Files:
2 edited

Legend:

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

    r50810 r50815  
    724724
    725725        if ( ! empty( $info['APP13'] ) ) {
    726             if (
    727                 // Skip when running unit tests.
    728                 ! defined( 'WP_RUN_CORE_TESTS' )
    729                 &&
    730                 // Process without silencing errors when in debug mode.
    731                 defined( 'WP_DEBUG' ) && WP_DEBUG
     726            // Don't silence errors when in debug mode, unless running unit tests.
     727            if ( defined( 'WP_DEBUG' ) && WP_DEBUG
     728                && ! defined( 'WP_RUN_CORE_TESTS' )
    732729            ) {
    733730                $iptc = iptcparse( $info['APP13'] );
     
    795792
    796793    if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) {
    797         if (
    798             // Skip when running unit tests.
    799             ! defined( 'WP_RUN_CORE_TESTS' )
    800             &&
    801             // Process without silencing errors when in debug mode.
    802             defined( 'WP_DEBUG' ) && WP_DEBUG
     794        // Don't silence errors when in debug mode, unless running unit tests.
     795        if ( defined( 'WP_DEBUG' ) && WP_DEBUG
     796            && ! defined( 'WP_RUN_CORE_TESTS' )
    803797        ) {
    804798            $exif = exif_read_data( $file );
  • trunk/src/wp-includes/media.php

    r50814 r50815  
    49884988 */
    49894989function wp_getimagesize( $filename, array &$image_info = null ) {
    4990     if (
    4991         // Skip when running unit tests.
    4992         ! defined( 'WP_RUN_CORE_TESTS' )
    4993         &&
    4994         // Return without silencing errors when in debug mode.
    4995         defined( 'WP_DEBUG' ) && WP_DEBUG
     4990    // Don't silence errors when in debug mode, unless running unit tests.
     4991    if ( defined( 'WP_DEBUG' ) && WP_DEBUG
     4992        && ! defined( 'WP_RUN_CORE_TESTS' )
    49964993    ) {
    49974994        if ( 2 === func_num_args() ) {
    4998             return _wp_get_image_size( $filename, $image_info );
     4995            $info = getimagesize( $filename, $image_info );
    49994996        } else {
    5000             return _wp_get_image_size( $filename );
    5001         }
    5002     }
    5003 
    5004     /*
    5005      * Silencing notice and warning is intentional.
    5006      *
    5007      * getimagesize() has a tendency to generate errors, such as
    5008      * "corrupt JPEG data: 7191 extraneous bytes before marker",
    5009      * even when it's able to provide image size information.
    5010      *
    5011      * See https://core.trac.wordpress.org/ticket/42480
    5012      */
    5013     if ( 2 === func_num_args() ) {
    5014         // phpcs:ignore WordPress.PHP.NoSilencedErrors
    5015         return @_wp_get_image_size( $filename, $image_info );
     4997            $info = getimagesize( $filename );
     4998        }
    50164999    } else {
    5017         // phpcs:ignore WordPress.PHP.NoSilencedErrors
    5018         return @_wp_get_image_size( $filename );
    5019     }
     5000        /*
     5001         * Silencing notice and warning is intentional.
     5002         *
     5003         * getimagesize() has a tendency to generate errors, such as
     5004         * "corrupt JPEG data: 7191 extraneous bytes before marker",
     5005         * even when it's able to provide image size information.
     5006         *
     5007         * See https://core.trac.wordpress.org/ticket/42480
     5008         */
     5009        if ( 2 === func_num_args() ) {
     5010            // phpcs:ignore WordPress.PHP.NoSilencedErrors
     5011            $info = @getimagesize( $filename, $image_info );
     5012        } else {
     5013            // phpcs:ignore WordPress.PHP.NoSilencedErrors
     5014            $info = @getimagesize( $filename );
     5015        }
     5016    }
     5017
     5018    if ( false !== $info ) {
     5019        return $info;
     5020    }
     5021
     5022    // For PHP versions that don't support WebP images,
     5023    // extract the image size info from the file headers.
     5024    if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
     5025        $webp_info = wp_get_webp_info( $filename );
     5026        $width     = $webp_info['width'];
     5027        $height    = $webp_info['height'];
     5028
     5029        // Mimic the native return format.
     5030        if ( $width && $height ) {
     5031            return array(
     5032                $width,
     5033                $height,
     5034                IMAGETYPE_WEBP, // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound
     5035                sprintf(
     5036                    'width="%d" height="%d"',
     5037                    $width,
     5038                    $height
     5039                ),
     5040                'mime' => 'image/webp',
     5041            );
     5042        }
     5043    }
     5044
     5045    // The image could not be parsed.
     5046    return false;
    50205047}
    50215048
     
    51045131    return $type && 'lossy' === $type;
    51055132}
    5106 
    5107 /**
    5108  * Gets the image size, with support for WebP images.
    5109  *
    5110  * @since 5.8.0
    5111  * @access private
    5112  *
    5113  * @param string $filename  The file path.
    5114  * @param array  $imageinfo Extended image information, passed by reference.
    5115  * @return array|false Array of image information or false on failure.
    5116  */
    5117 function _wp_get_image_size( $filename, &$imageinfo = array() ) {
    5118     // Try getimagesize() first.
    5119     $info = getimagesize( $filename, $imageinfo );
    5120 
    5121     if ( false !== $info ) {
    5122         return $info;
    5123     }
    5124 
    5125     // For PHP versions that don't support WebP images,
    5126     // extract the image size info from the file headers.
    5127     if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
    5128         $webp_info = wp_get_webp_info( $filename );
    5129         $width     = $webp_info['width'];
    5130         $height    = $webp_info['height'];
    5131 
    5132         // Mimic the native return format.
    5133         if ( $width && $height ) {
    5134             return array(
    5135                 $width,
    5136                 $height,
    5137                 IMAGETYPE_WEBP, // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound
    5138                 sprintf(
    5139                     'width="%d" height="%d"',
    5140                     $width,
    5141                     $height
    5142                 ),
    5143                 'mime' => 'image/webp',
    5144             );
    5145         }
    5146     }
    5147 
    5148     // The image could not be parsed.
    5149     return false;
    5150 }
Note: See TracChangeset for help on using the changeset viewer.