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

@@ -105,7 +105,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
}
$size = @getimagesize( $this->file );
$size = wp_getimagesize( $this->file );
if ( ! $size ) {
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );

View File

@@ -1948,7 +1948,7 @@ function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
// Do we need to constrain the image?
if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
$imagesize = @getimagesize($src_file);
$imagesize = wp_getimagesize($src_file);
if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
$actual_aspect = $imagesize[0] / $imagesize[1];

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 );
}

View File

@@ -244,7 +244,7 @@ function image_downsize( $id, $size = 'medium' ) {
$info = null;
if ( $thumb_file ) {
$info = @getimagesize( $thumb_file );
$info = wp_getimagesize( $thumb_file );
}
if ( $thumb_file && $info ) {
@@ -962,7 +962,7 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
$src_file = $icon_dir . '/' . wp_basename( $src );
list( $width, $height ) = @getimagesize( $src_file );
list( $width, $height ) = wp_getimagesize( $src_file );
}
}