diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index d4b40afc7bb6b..267cf0e3e3cad 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -806,6 +806,33 @@ function wp_exif_date2ts( $str ) { return strtotime( "{$y}-{$m}-{$d} {$time}" ); } +/** + * Determines whether a WebP image declares Exif metadata. + * + * Exif metadata can only be stored in the extended WebP file format, which + * starts with a `VP8X` chunk holding a flag for the presence of Exif data. + * + * @since 7.3.0 + * @access private + * + * @param string $file Path to the WebP image. + * @return bool True if the image declares Exif metadata, false otherwise. + */ +function _wp_webp_has_exif_data( $file ) { + $header = file_get_contents( $file, false, null, 0, 21 ); + + if ( false === $header || strlen( $header ) < 21 ) { + return false; + } + + if ( 'RIFF' !== substr( $header, 0, 4 ) || 'WEBPVP8X' !== substr( $header, 8, 8 ) ) { + return false; + } + + // The Exif flag is the fourth bit of the first byte of the VP8X chunk data. + return (bool) ( ord( $header[20] ) & 0x08 ); +} + /** * Gets extended image metadata, exif or iptc as available. * @@ -931,17 +958,35 @@ function wp_read_image_metadata( $file ) { $exif = array(); + $exif_image_types = array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ); + + // As of PHP 8.6, exif_read_data() can read Exif data from WebP images. + if ( PHP_VERSION_ID >= 80600 ) { + $exif_image_types[] = IMAGETYPE_WEBP; + } + /** * Filters the image types to check for exif data. * * @since 2.5.0 + * @since 7.3.0 `IMAGETYPE_WEBP` is included by default on PHP 8.6 and later. * * @param int[] $image_types Array of image types to check for exif data. Each value * is usually one of the `IMAGETYPE_*` constants. */ - $exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ); + $exif_image_types = apply_filters( 'wp_read_image_metadata_types', $exif_image_types ); + + $read_exif = is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ); + + /* + * exif_read_data() raises an "Invalid WebP file" warning for a WebP image + * that contains no Exif data, so only WebP images that declare some are read. + */ + if ( $read_exif && IMAGETYPE_WEBP === $image_type && ! _wp_webp_has_exif_data( $file ) ) { + $read_exif = false; + } - if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { + if ( $read_exif ) { // Don't silence errors when in debug mode, unless running unit tests. if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) diff --git a/tests/phpunit/data/images/webp-exif.webp b/tests/phpunit/data/images/webp-exif.webp new file mode 100644 index 0000000000000..801fffb14dab8 Binary files /dev/null and b/tests/phpunit/data/images/webp-exif.webp differ diff --git a/tests/phpunit/tests/image/meta.php b/tests/phpunit/tests/image/meta.php index c9d0840e72b37..862e4c9e7e6d1 100644 --- a/tests/phpunit/tests/image/meta.php +++ b/tests/phpunit/tests/image/meta.php @@ -181,6 +181,117 @@ public function test_iptc_alt() { $this->assertSame( 'This is the Alt Text description to support accessibility in 2025.1', $out['alt'], 'Alt text does not match source.' ); } + /** + * Tests that Exif data is read from a WebP image when PHP supports it. + * + * @ticket 66789 + * + * @requires PHP >= 8.6 + */ + public function test_exif_webp() { + $out = wp_read_image_metadata( DIR_TESTDATA . '/images/webp-exif.webp' ); + + $this->assertSame( 'Synthetic test author', $out['credit'], 'The credit should be read from the Exif data.' ); + $this->assertSame( 'Synthetic test copyright', $out['copyright'], 'The copyright should be read from the Exif data.' ); + $this->assertSame( '6', (string) $out['orientation'], 'The orientation should be read from the Exif data.' ); + } + + /** + * Tests that WebP is only one of the default image types read for Exif data on PHP 8.6 and later. + * + * @ticket 66789 + */ + public function test_webp_is_a_default_exif_image_type_as_of_php_8_6() { + $image_types = null; + + add_filter( + 'wp_read_image_metadata_types', + static function ( $types ) use ( &$image_types ) { + $image_types = $types; + return $types; + } + ); + + wp_read_image_metadata( DIR_TESTDATA . '/images/webp-exif.webp' ); + + $this->assertIsArray( $image_types, 'The filter should have been applied.' ); + $this->assertContains( IMAGETYPE_JPEG, $image_types, 'JPEG should be a default image type.' ); + + if ( PHP_VERSION_ID >= 80600 ) { + $this->assertContains( IMAGETYPE_WEBP, $image_types, 'WebP should be a default image type on PHP 8.6 and later.' ); + } else { + $this->assertNotContains( IMAGETYPE_WEBP, $image_types, 'WebP should not be a default image type before PHP 8.6.' ); + } + } + + /** + * Tests that a WebP image without Exif data is not passed to exif_read_data(). + * + * On PHP 8.6.0, exif_read_data() raises an "Invalid WebP file" warning for such an image. + * The warning is silenced while running the tests, but still reaches a custom error handler. + * + * @ticket 66789 + * + * @dataProvider data_webp_without_exif + * + * @param string $file Name of a WebP image that has no Exif data. + */ + public function test_webp_without_exif_does_not_raise_a_warning( $file ) { + add_filter( + 'wp_read_image_metadata_types', + static function ( $types ) { + $types[] = IMAGETYPE_WEBP; + return $types; + } + ); + + $errors = array(); + + set_error_handler( + static function ( $errno, $errstr ) use ( &$errors ) { + $errors[] = $errstr; + return true; + } + ); + + $out = wp_read_image_metadata( DIR_TESTDATA . '/images/' . $file ); + + restore_error_handler(); + + $this->assertSame( array(), $errors, 'No error should be raised.' ); + $this->assertIsArray( $out, 'The image metadata should be returned.' ); + $this->assertSame( '', $out['credit'], 'The credit should be empty.' ); + $this->assertSame( 0, (int) $out['orientation'], 'The orientation should be the default.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_webp_without_exif() { + return array( + 'lossy' => array( 'webp-lossy.webp' ), + 'lossless' => array( 'webp-lossless.webp' ), + 'extended with alpha' => array( 'webp-transparent.webp' ), + 'extended with animation' => array( 'webp-animated.webp' ), + ); + } + + /** + * Tests the detection of Exif data in WebP images. + * + * @ticket 66789 + * + * @covers ::_wp_webp_has_exif_data + */ + public function test_wp_webp_has_exif_data() { + $this->assertTrue( _wp_webp_has_exif_data( DIR_TESTDATA . '/images/webp-exif.webp' ), 'A WebP image with the Exif flag should be detected.' ); + $this->assertFalse( _wp_webp_has_exif_data( DIR_TESTDATA . '/images/webp-transparent.webp' ), 'An extended WebP image without the Exif flag should not be detected.' ); + $this->assertFalse( _wp_webp_has_exif_data( DIR_TESTDATA . '/images/webp-lossy.webp' ), 'A simple WebP image should not be detected.' ); + $this->assertFalse( _wp_webp_has_exif_data( DIR_TESTDATA . '/images/test-image.jpg' ), 'A JPEG image should not be detected.' ); + } + /** * @ticket 9417 */