Skip to content

Commit 01df532

Browse files
committed
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
1 parent 0b9cff2 commit 01df532

8 files changed

Lines changed: 72 additions & 17 deletions

File tree

src/wp-admin/includes/ajax-actions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3927,7 +3927,7 @@ function wp_ajax_crop_image() {
39273927
$parent_url = wp_get_attachment_url( $attachment_id );
39283928
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
39293929

3930-
$size = @getimagesize( $cropped );
3930+
$size = wp_getimagesize( $cropped );
39313931
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
39323932

39333933
$object = array(

src/wp-admin/includes/class-custom-image-header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ public function step_2() {
791791
}
792792

793793
if ( file_exists( $file ) ) {
794-
list( $width, $height, $type, $attr ) = @getimagesize( $file );
794+
list( $width, $height, $type, $attr ) = wp_getimagesize( $file );
795795
} else {
796796
$data = wp_get_attachment_metadata( $attachment_id );
797797
$height = isset( $data['height'] ) ? $data['height'] : 0;
@@ -1223,7 +1223,7 @@ final public function create_attachment_object( $cropped, $parent_attachment_id
12231223
$parent_url = wp_get_attachment_url( $parent->ID );
12241224
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
12251225

1226-
$size = @getimagesize( $cropped );
1226+
$size = wp_getimagesize( $cropped );
12271227
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
12281228

12291229
$object = array(

src/wp-admin/includes/class-wp-site-icon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function create_attachment_object( $cropped, $parent_attachment_id ) {
8787
$parent_url = wp_get_attachment_url( $parent->ID );
8888
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
8989

90-
$size = @getimagesize( $cropped );
90+
$size = wp_getimagesize( $cropped );
9191
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
9292

9393
$object = array(

src/wp-admin/includes/image.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function wp_get_missing_image_subsizes( $attachment_id ) {
9393
// Use the originally uploaded image dimensions as full_width and full_height.
9494
if ( ! empty( $image_meta['original_image'] ) ) {
9595
$image_file = wp_get_original_image_path( $attachment_id );
96-
$imagesize = @getimagesize( $image_file );
96+
$imagesize = wp_getimagesize( $image_file );
9797
}
9898

9999
if ( ! empty( $imagesize ) ) {
@@ -224,7 +224,7 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me
224224
* @return array The image attachment meta data.
225225
*/
226226
function wp_create_image_subsizes( $file, $attachment_id ) {
227-
$imagesize = @getimagesize( $file );
227+
$imagesize = wp_getimagesize( $file );
228228

229229
if ( empty( $imagesize ) ) {
230230
// File is not an image.
@@ -687,7 +687,7 @@ function wp_read_image_metadata( $file ) {
687687
return false;
688688
}
689689

690-
list( , , $image_type ) = @getimagesize( $file );
690+
list( , , $image_type ) = wp_getimagesize( $file );
691691

692692
/*
693693
* EXIF contains a bunch of data we'll probably never need formatted in ways
@@ -716,10 +716,21 @@ function wp_read_image_metadata( $file ) {
716716
* as caption, description etc.
717717
*/
718718
if ( is_callable( 'iptcparse' ) ) {
719-
@getimagesize( $file, $info );
719+
wp_getimagesize( $file, $info );
720720

721721
if ( ! empty( $info['APP13'] ) ) {
722-
$iptc = @iptcparse( $info['APP13'] );
722+
if (
723+
// Skip when running unit tests.
724+
! defined( 'DIR_TESTDATA' )
725+
&&
726+
// Process without silencing errors when in debug mode.
727+
defined( 'WP_DEBUG' ) && WP_DEBUG
728+
) {
729+
$iptc = iptcparse( $info['APP13'] );
730+
} else {
731+
// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
732+
$iptc = @iptcparse( $info['APP13'] );
733+
}
723734

724735
// Headline, "A brief synopsis of the caption".
725736
if ( ! empty( $iptc['2#105'][0] ) ) {
@@ -779,7 +790,18 @@ function wp_read_image_metadata( $file ) {
779790
$exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) );
780791

781792
if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) {
782-
$exif = @exif_read_data( $file );
793+
if (
794+
// Skip when running unit tests.
795+
! defined( 'DIR_TESTDATA' )
796+
&&
797+
// Process without silencing errors when in debug mode.
798+
defined( 'WP_DEBUG' ) && WP_DEBUG
799+
) {
800+
$exif = exif_read_data( $file );
801+
} else {
802+
// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
803+
$exif = @exif_read_data( $file );
804+
}
783805

784806
if ( ! empty( $exif['ImageDescription'] ) ) {
785807
mbstring_binary_safe_encoding();
@@ -877,7 +899,7 @@ function wp_read_image_metadata( $file ) {
877899
* @return bool True if valid image, false if not valid image.
878900
*/
879901
function file_is_valid_image( $path ) {
880-
$size = @getimagesize( $path );
902+
$size = wp_getimagesize( $path );
881903
return ! empty( $size );
882904
}
883905

@@ -892,7 +914,7 @@ function file_is_valid_image( $path ) {
892914
function file_is_displayable_image( $path ) {
893915
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO );
894916

895-
$info = @getimagesize( $path );
917+
$info = wp_getimagesize( $path );
896918
if ( empty( $info ) ) {
897919
$result = false;
898920
} elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {

src/wp-includes/class-wp-image-editor-gd.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function load() {
105105
return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
106106
}
107107

108-
$size = @getimagesize( $this->file );
108+
$size = wp_getimagesize( $this->file );
109109

110110
if ( ! $size ) {
111111
return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );

src/wp-includes/deprecated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
19481948
// Do we need to constrain the image?
19491949
if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
19501950

1951-
$imagesize = @getimagesize($src_file);
1951+
$imagesize = wp_getimagesize($src_file);
19521952

19531953
if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
19541954
$actual_aspect = $imagesize[0] / $imagesize[1];

src/wp-includes/functions.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3052,7 +3052,7 @@ function wp_get_image_mime( $file ) {
30523052
$imagetype = exif_imagetype( $file );
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 {
30583058
$mime = false;
@@ -7866,3 +7866,36 @@ function is_php_version_compatible( $required ) {
78667866
function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
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+
*/
7879+
function 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+
}

src/wp-includes/media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function image_downsize( $id, $size = 'medium' ) {
244244
$info = null;
245245

246246
if ( $thumb_file ) {
247-
$info = @getimagesize( $thumb_file );
247+
$info = wp_getimagesize( $thumb_file );
248248
}
249249

250250
if ( $thumb_file && $info ) {
@@ -962,7 +962,7 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
962962
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
963963

964964
$src_file = $icon_dir . '/' . wp_basename( $src );
965-
list( $width, $height ) = @getimagesize( $src_file );
965+
list( $width, $height ) = wp_getimagesize( $src_file );
966966
}
967967
}
968968

0 commit comments

Comments
 (0)