From 0778849c122757af522f31ec566bc57f53f7fef6 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 5 May 2021 17:08:01 +0000 Subject: [PATCH] Media: Some documentation and test improvements for WebP support: * Document that WebP constants are only defined in PHP 7.1+. * Correct the `$filename` parameter type in `wp_get_webp_info()`. * Use a consistent message when skipping tests due to the lack of WebP support. * Remove unnecessary `else` branches after `markTestSkipped()`. * Replace `assertEquals()` with more appropriate assertions. Follow-up to [50810]. See #35725. Built from https://develop.svn.wordpress.org/trunk@50814 git-svn-id: http://core.svn.wordpress.org/trunk@50423 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/compat.php | 2 +- wp-includes/functions.php | 8 +++++--- wp-includes/media.php | 14 ++++++++++---- wp-includes/version.php | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/wp-includes/compat.php b/wp-includes/compat.php index 59e5700439..aec742d77a 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -371,7 +371,7 @@ if ( ! function_exists( 'is_iterable' ) ) { } } -// WebP constants may not be defined, even in cases where the format is supported. +// WebP constants are only defined in PHP 7.1+. if ( ! defined( 'IMAGETYPE_WEBP' ) ) { define( 'IMAGETYPE_WEBP', 18 ); } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 3bb257ab8b..d8e90d82c7 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3079,9 +3079,11 @@ function wp_get_image_mime( $file ) { return false; } - // Add WebP fallback detection when image library doesn't support WebP. - // Note: detection values come from LibWebP, see - // https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30 + /* + * Add WebP fallback detection when image library doesn't support WebP. + * Note: detection values come from LibWebP, see + * https://github.com/webmproject/libwebp/blob/master/imageio/image_dec.c#L30 + */ $magic = bin2hex( $magic ); if ( // RIFF. diff --git a/wp-includes/media.php b/wp-includes/media.php index 82d1b32627..100fe5dfab 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -5024,7 +5024,7 @@ function wp_getimagesize( $filename, array &$image_info = null ) { * * @since 5.8.0 * - * @param [type] $filename Path to a WebP file. + * @param string $filename Path to a WebP file. * @return array $webp_info { * An array of WebP image information. * @@ -5038,9 +5038,11 @@ function wp_get_webp_info( $filename ) { $width = false; $height = false; $type = false; + if ( ! 'image/webp' === wp_get_image_mime( $filename ) ) { return compact( 'width', 'height', 'type' ); } + try { $handle = fopen( $filename, 'rb' ); if ( $handle ) { @@ -5083,6 +5085,7 @@ function wp_get_webp_info( $filename ) { } } catch ( Exception $e ) { } + return compact( 'width', 'height', 'type' ); } @@ -5097,6 +5100,7 @@ function wp_get_webp_info( $filename ) { function _wp_webp_is_lossy( $filename ) { $webp_info = wp_get_webp_info( $filename ); $type = $webp_info['type']; + return $type && 'lossy' === $type; } @@ -5113,17 +5117,19 @@ function _wp_webp_is_lossy( $filename ) { 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. + + // 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. + // Mimic the native return format. if ( $width && $height ) { return array( $width, diff --git a/wp-includes/version.php b/wp-includes/version.php index d10ca5ed0d..3a9d807c54 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50813'; +$wp_version = '5.8-alpha-50814'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.