From 16c42e467d654c4d4fbc0ee6e63c101429d062c9 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 5 May 2021 18:47:57 +0000 Subject: [PATCH] Media: Move retrieving WebP image size information into `wp_getimagesize()`. Remove `_wp_get_image_size()`. Follow-up to [50146], [50810], [50814]. Props johnjamesjacoby. See #35725. Built from https://develop.svn.wordpress.org/trunk@50815 git-svn-id: http://core.svn.wordpress.org/trunk@50424 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 18 ++---- wp-includes/media.php | 118 +++++++++++++++--------------------- wp-includes/version.php | 2 +- 3 files changed, 57 insertions(+), 81 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 142c245f89..b3b68560b4 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -723,12 +723,9 @@ function wp_read_image_metadata( $file ) { wp_getimagesize( $file, $info ); if ( ! empty( $info['APP13'] ) ) { - if ( - // Skip when running unit tests. - ! defined( 'WP_RUN_CORE_TESTS' ) - && - // Process without silencing errors when in debug mode. - defined( 'WP_DEBUG' ) && WP_DEBUG + // Don't silence errors when in debug mode, unless running unit tests. + if ( defined( 'WP_DEBUG' ) && WP_DEBUG + && ! defined( 'WP_RUN_CORE_TESTS' ) ) { $iptc = iptcparse( $info['APP13'] ); } else { @@ -794,12 +791,9 @@ function wp_read_image_metadata( $file ) { $exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ); if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { - if ( - // Skip when running unit tests. - ! defined( 'WP_RUN_CORE_TESTS' ) - && - // Process without silencing errors when in debug mode. - defined( 'WP_DEBUG' ) && WP_DEBUG + // Don't silence errors when in debug mode, unless running unit tests. + if ( defined( 'WP_DEBUG' ) && WP_DEBUG + && ! defined( 'WP_RUN_CORE_TESTS' ) ) { $exif = exif_read_data( $file ); } else { diff --git a/wp-includes/media.php b/wp-includes/media.php index 100fe5dfab..f799ba109e 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -4987,36 +4987,63 @@ function wp_show_heic_upload_error( $plupload_settings ) { * @return array|false Array of image information or false on failure. */ function wp_getimagesize( $filename, array &$image_info = null ) { - if ( - // Skip when running unit tests. - ! defined( 'WP_RUN_CORE_TESTS' ) - && - // Return without silencing errors when in debug mode. - defined( 'WP_DEBUG' ) && WP_DEBUG + // Don't silence errors when in debug mode, unless running unit tests. + if ( defined( 'WP_DEBUG' ) && WP_DEBUG + && ! defined( 'WP_RUN_CORE_TESTS' ) ) { if ( 2 === func_num_args() ) { - return _wp_get_image_size( $filename, $image_info ); + $info = getimagesize( $filename, $image_info ); } else { - return _wp_get_image_size( $filename ); + $info = getimagesize( $filename ); + } + } else { + /* + * 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 + */ + if ( 2 === func_num_args() ) { + // phpcs:ignore WordPress.PHP.NoSilencedErrors + $info = @getimagesize( $filename, $image_info ); + } else { + // phpcs:ignore WordPress.PHP.NoSilencedErrors + $info = @getimagesize( $filename ); } } - /* - * 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 - */ - if ( 2 === func_num_args() ) { - // phpcs:ignore WordPress.PHP.NoSilencedErrors - return @_wp_get_image_size( $filename, $image_info ); - } else { - // phpcs:ignore WordPress.PHP.NoSilencedErrors - return @_wp_get_image_size( $filename ); + if ( false !== $info ) { + return $info; } + + // For PHP versions that don't support WebP images, + // extract the image size info from the file headers. + if ( 'image/webp' === wp_get_image_mime( $filename ) ) { + $webp_info = wp_get_webp_info( $filename ); + $width = $webp_info['width']; + $height = $webp_info['height']; + + // Mimic the native return format. + if ( $width && $height ) { + return array( + $width, + $height, + IMAGETYPE_WEBP, // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound + sprintf( + 'width="%d" height="%d"', + $width, + $height + ), + 'mime' => 'image/webp', + ); + } + } + + // The image could not be parsed. + return false; } /** @@ -5103,48 +5130,3 @@ function _wp_webp_is_lossy( $filename ) { return $type && 'lossy' === $type; } - -/** - * Gets the image size, with support for WebP images. - * - * @since 5.8.0 - * @access private - * - * @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_get_image_size( $filename, &$imageinfo = array() ) { - // Try getimagesize() first. - $info = getimagesize( $filename, $imageinfo ); - - if ( false !== $info ) { - return $info; - } - - // For PHP versions that don't support WebP images, - // extract the image size info from the file headers. - if ( 'image/webp' === wp_get_image_mime( $filename ) ) { - $webp_info = wp_get_webp_info( $filename ); - $width = $webp_info['width']; - $height = $webp_info['height']; - - // Mimic the native return format. - if ( $width && $height ) { - return array( - $width, - $height, - IMAGETYPE_WEBP, // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound - sprintf( - 'width="%d" height="%d"', - $width, - $height - ), - 'mime' => 'image/webp', - ); - } - } - - // The image could not be parsed. - return false; -} diff --git a/wp-includes/version.php b/wp-includes/version.php index 3a9d807c54..c9c566d613 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50814'; +$wp_version = '5.8-alpha-50815'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.