From 56ee8ec360d91183a90788a8ff5009bced5dc6c4 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 14:17:50 +0000 Subject: [PATCH 1/2] Media: Read Exif data from WebP images on PHP 8.6 and later. As of PHP 8.6, `exif_read_data()` can read Exif data from WebP images. `wp_read_image_metadata()` only passes JPEG and TIFF images to it by default, so a WebP image carrying Exif data gets no credit, copyright, camera or orientation metadata. `IMAGETYPE_WEBP` is now included in the default list passed to the `wp_read_image_metadata_types` filter when running on PHP 8.6 or later. Earlier versions are unchanged. --- src/wp-admin/includes/image.php | 10 +++++++- tests/phpunit/data/images/webp-exif.webp | Bin 0 -> 162 bytes tests/phpunit/tests/image/meta.php | 30 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/data/images/webp-exif.webp diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index d4b40afc7bb6b..6b3ff2f23fe6b 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -931,15 +931,23 @@ 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 ); if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { // Don't silence errors when in debug mode, unless running unit tests. diff --git a/tests/phpunit/data/images/webp-exif.webp b/tests/phpunit/data/images/webp-exif.webp new file mode 100644 index 0000000000000000000000000000000000000000..801fffb14dab8e148c9e2493219c0d36d6944696 GIT binary patch literal 162 zcmWIYbaR`J$(bU=hIuWOD#9BLf2y0|QXRM-a%D z3=FOjo^DA%8DC!pEg;3gz|1Jbzzk$DGOz&&YmjaRF(5W-ngM1@0@)rw%n)3eSCWxh gl9{Yfl3HA%kXTxhkza%^nw(!yS(KTcQNq9g0D;jW!2kdN literal 0 HcmV?d00001 diff --git a/tests/phpunit/tests/image/meta.php b/tests/phpunit/tests/image/meta.php index c9d0840e72b37..4f4546a6f6a93 100644 --- a/tests/phpunit/tests/image/meta.php +++ b/tests/phpunit/tests/image/meta.php @@ -181,6 +181,36 @@ 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 a WebP image is not passed to exif_read_data() on PHP versions that cannot read it. + * + * @ticket 66789 + * + * @requires PHP < 8.6 + */ + public function test_exif_webp_is_not_read_before_php_8_6() { + $out = wp_read_image_metadata( DIR_TESTDATA . '/images/webp-exif.webp' ); + + $this->assertSame( '', $out['credit'], 'The credit should be empty.' ); + $this->assertSame( '', $out['copyright'], 'The copyright should be empty.' ); + $this->assertSame( '0', (string) $out['orientation'], 'The orientation should be the default.' ); + } + /** * @ticket 9417 */ From 3db8d88770c9946beb5b1e6b5601eea3063c9d29 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 17:53:07 +0000 Subject: [PATCH 2/2] Media: Only read Exif data from WebP images that declare it. On PHP 8.6.0, `exif_read_data()` raises an "Invalid WebP file" warning and returns `false` for a WebP image that has no Exif chunk. `wp_read_image_metadata()` now checks the Exif flag in the `VP8X` header through a new private function, `_wp_webp_has_exif_data()`, and skips `exif_read_data()` for a WebP image without it. The test that could not fail is replaced by one that checks the default image types passed to the `wp_read_image_metadata_types` filter, and a new test confirms that WebP images without Exif data raise no error. Follow-up to review feedback from desrosj. See #66789. --- src/wp-admin/includes/image.php | 39 ++++++++++++- tests/phpunit/tests/image/meta.php | 93 ++++++++++++++++++++++++++++-- 2 files changed, 125 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 6b3ff2f23fe6b..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. * @@ -949,7 +976,17 @@ function wp_read_image_metadata( $file ) { */ $exif_image_types = apply_filters( 'wp_read_image_metadata_types', $exif_image_types ); - if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { + $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 ( $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/tests/image/meta.php b/tests/phpunit/tests/image/meta.php index 4f4546a6f6a93..862e4c9e7e6d1 100644 --- a/tests/phpunit/tests/image/meta.php +++ b/tests/phpunit/tests/image/meta.php @@ -197,18 +197,99 @@ public function test_exif_webp() { } /** - * Tests that a WebP image is not passed to exif_read_data() on PHP versions that cannot read it. + * 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. * - * @requires PHP < 8.6 + * @ticket 66789 + * + * @dataProvider data_webp_without_exif + * + * @param string $file Name of a WebP image that has no Exif data. */ - public function test_exif_webp_is_not_read_before_php_8_6() { - $out = wp_read_image_metadata( DIR_TESTDATA . '/images/webp-exif.webp' ); + 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( '', $out['copyright'], 'The copyright should be empty.' ); - $this->assertSame( '0', (string) $out['orientation'], 'The orientation should be the default.' ); + $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.' ); } /**