WordPress.org

Make WordPress Core

Changeset 50146


Ignore:
Timestamp:
02/02/2021 04:51:17 PM (8 months 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.

Location:
trunk/src
Files:
8 edited

Legend:

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

    r50129 r50146  
    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
  • trunk/src/wp-admin/includes/class-custom-image-header.php

    r49384 r50146  
    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 );
     
    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
  • trunk/src/wp-admin/includes/class-wp-site-icon.php

    r49692 r50146  
    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
  • trunk/src/wp-admin/includes/image.php

    r49936 r50146  
    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
     
    225225 */
    226226function wp_create_image_subsizes( $file, $attachment_id ) {
    227     $imagesize = @getimagesize( $file );
     227    $imagesize = wp_getimagesize( $file );
    228228
    229229    if ( empty( $imagesize ) ) {
     
    688688    }
    689689
    690     list( , , $image_type ) = @getimagesize( $file );
     690    list( , , $image_type ) = wp_getimagesize( $file );
    691691
    692692    /*
     
    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".
     
    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'] ) ) {
     
    878900 */
    879901function file_is_valid_image( $path ) {
    880     $size = @getimagesize( $path );
     902    $size = wp_getimagesize( $path );
    881903    return ! empty( $size );
    882904}
     
    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;
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r49927 r50146  
    106106        }
    107107
    108         $size = @getimagesize( $this->file );
     108        $size = wp_getimagesize( $this->file );
    109109
    110110        if ( ! $size ) {
  • trunk/src/wp-includes/deprecated.php

    r49992 r50146  
    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] ) {
  • 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}
  • trunk/src/wp-includes/media.php

    r50144 r50146  
    245245
    246246        if ( $thumb_file ) {
    247             $info = @getimagesize( $thumb_file );
     247            $info = wp_getimagesize( $thumb_file );
    248248        }
    249249
     
    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        }
Note: See TracChangeset for help on using the changeset viewer.