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.


git-svn-id: https://develop.svn.wordpress.org/trunk@50146 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Anthony Burchell
2021-02-02 16:51:17 +00:00
parent 0b9cff29e4
commit 01df5322ef
8 changed files with 72 additions and 17 deletions

View File

@@ -3052,7 +3052,7 @@ function wp_get_image_mime( $file ) {
$imagetype = exif_imagetype( $file );
$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false;
} elseif ( function_exists( 'getimagesize' ) ) {
$imagesize = @getimagesize( $file );
$imagesize = wp_getimagesize( $file );
$mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
} else {
$mime = false;
@@ -7866,3 +7866,36 @@ function is_php_version_compatible( $required ) {
function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
return abs( (float) $expected - (float) $actual ) <= $precision;
}
/**
* Allows PHP's getimagesize() to be debuggable when necessary.
*
* @since 5.7.0
*
* @param string $filename The file path.
* @param array $imageinfo Extended image information, passed by reference.
* @return array|false Array of image information or false on failure.
*/
function wp_getimagesize( $filename, &$imageinfo = array() ) {
if (
// Skip when running unit tests.
! defined( 'DIR_TESTDATA' )
&&
// Return without silencing errors when in debug mode.
defined( 'WP_DEBUG' ) && WP_DEBUG
) {
return getimagesize( $filename, $imageinfo );
}
/**
* Silencing notice and warning is intentional.
*
* getimagesize() has a tendency to generate errors, such as "corrupt JPEG data: 7191 extraneous bytes before
* marker", even when it's able to provide image size information.
*
* See https://core.trac.wordpress.org/ticket/42480
*
* phpcs:ignore WordPress.PHP.NoSilencedErrors
*/
return @getimagesize( $filename, $imageinfo );
}