Make WordPress Core


Ignore:
Timestamp:
02/02/2021 04:51:17 PM (4 years ago)
Author:
antpb
Message:

Media: Avoid suppressing errors when using getimagesize().

Previously, all logic utilizing getimagesize() was supressing errors making it difficult to debug usage of the function.

A new wp_getimagesize() function has been added to allow the errors to no longer be suppressed when WP_DEBUG is enabled.

Props Howdy_McGee, SergeyBiryukov, mukesh27, davidbaumwald, noisysocks, hellofromTonya.
Fixes #49889.

File:
1 edited

Legend:

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

    r50140 r50146  
    30533053            $mime      = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
    30543054        } elseif ( function_exists( 'getimagesize' ) ) {
    3055             $imagesize = @getimagesize( $file );
     3055            $imagesize = wp_getimagesize( $file );
    30563056            $mime      = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
    30573057        } else {
     
    78677867    return abs( (float) $expected - (float) $actual ) <= $precision;
    78687868}
     7869
     7870/**
     7871 * Allows PHP's getimagesize() to be debuggable when necessary.
     7872 *
     7873 * @since 5.7.0
     7874 *
     7875 * @param string $filename The file path.
     7876 * @param array $imageinfo Extended image information, passed by reference.
     7877 * @return array|false Array of image information or false on failure.
     7878 */
     7879function wp_getimagesize( $filename, &$imageinfo = array() ) {
     7880    if (
     7881        // Skip when running unit tests.
     7882        ! defined( 'DIR_TESTDATA' )
     7883        &&
     7884        // Return without silencing errors when in debug mode.
     7885        defined( 'WP_DEBUG' ) && WP_DEBUG
     7886    ) {
     7887        return getimagesize( $filename, $imageinfo );
     7888    }
     7889
     7890    /**
     7891     * Silencing notice and warning is intentional.
     7892     *
     7893     * getimagesize() has a tendency to generate errors, such as "corrupt JPEG data: 7191 extraneous bytes before
     7894     * marker", even when it's able to provide image size information.
     7895     *
     7896     * See https://core.trac.wordpress.org/ticket/42480
     7897     *
     7898     * phpcs:ignore WordPress.PHP.NoSilencedErrors
     7899     */
     7900    return @getimagesize( $filename, $imageinfo );
     7901}
Note: See TracChangeset for help on using the changeset viewer.