Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' )
Expand Down
Binary file added tests/phpunit/data/images/webp-exif.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 111 additions & 0 deletions tests/phpunit/tests/image/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading