Skip to content

Media: Read Exif data from WebP images on PHP 8.6 and later - #16172

Open
AceMedia wants to merge 2 commits into
WordPress:trunkfrom
AceMedia:ticket/66789-webp-exif
Open

AceMedia wants to merge 2 commits into
WordPress:trunkfrom
AceMedia:ticket/66789-webp-exif

Conversation

@AceMedia

@AceMedia AceMedia commented Nov 23, 2026 •

Copy link
Copy Markdown

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, although the runtime can read it.

This includes IMAGETYPE_WEBP 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, and the filter can still remove the type.

On PHP 8.6.0, exif_read_data() raises an "Invalid WebP file" warning and returns false for a WebP image with no Exif chunk (reported upstream in php/php-src#25678). To avoid that, a WebP image is only passed to exif_read_data() when its VP8X header has the Exif flag set, checked by a new private function, _wp_webp_has_exif_data() (added after review feedback from @desrosj).

It adds a 162-byte synthetic WebP fixture with Exif data and tests for: reading Exif data from WebP on PHP 8.6 and later, the default image types passed to the filter on either side of 8.6, WebP images without Exif data raising no error, and the new function.

Testing

tests/image/meta.php, with the exif extension loaded:

PHP 8.5.12 PHP 8.6.0
New tests, without the Exif flag check 4 failures: the WebP images without Exif data raise a warning 4 failures: the same
This change Passes (25 tests, 119 assertions, 1 skip) Passes (25 tests, 122 assertions)

It also passes on PHP 8.4.25 (25 tests, 119 assertions, 1 skip). On PHP 8.6.0, --group image,media passes with this change (1,090 tests, 12,895 assertions, 54 skips). Before the review changes, the first version of the WebP test failed on PHP 8.6.0 with trunk's source because the credit was empty.

PHPCS passes on the changed files.

Notes for review

  • I used @since 7.3.0 because trunk is at 7.2-RC2. Please correct it if this should land elsewhere.
  • The existing fixture test-image-rotated-90cw.webp already has an orientation tag (8), so on PHP 8.6 its image_meta orientation would be stored as 8 rather than 0. Rotation is not changed here: WP_Image_Editor::maybe_exif_rotate() still only reads the tag for image/jpeg. That could be a follow-up.

Trac ticket: https://core.trac.wordpress.org/ticket/66789

Use of AI Tools

AI assistance: Yes
Tool(s): Codex, Claude Code
Used for: Assessing the PHP 8.6 change against Core, writing the change, the fixture, the tests and this description, and running the tests. The test results above are automated runs by those tools.

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.
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props shanerounce, desrosj.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@desrosj desrosj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good starting point, @AceMedia! A few things that I noticed while reviewing and testing.

First, I noticed that a E_WARNING "Invalid WebP file" is emitted when a WebP file does not have any EXIF data (which will likely be a large percentage). This is a bit different than how JPEGs are handled, returning an empty array with no warning. The printed warning text breaks sites when WP_DEBUG is on.

The tests are not catching it because WP_RUN_CORE_TESTS is defined in Core's test suite, which uses @ on the call to suppress notices and warnings. It looks like HEIF files also have this inconsistent behavior, so it should likely be flagged upstream in https://github.com/php/php-src/.

Next, it looks like test_exif_webp_is_not_read_before_php_8_6 will never fail. A better test would be to check the contents of default array to confirm that IMAGETYPE_WEBP is not present.

Lastly, I think it could be good to add the automatic rotation based on EXIF data (and anything else that is changed by reading that data) at the same time. But let's not add it just yet.

@desrosj

desrosj commented Nov 23, 2026

Copy link
Copy Markdown
Member

Looks like you've already flagged this upstream for WebP in php/php-src#25678 / php/php-src#25679. Thanks!

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.
@AceMedia

Copy link
Copy Markdown
Author

Thanks for testing this, both points were right.

I reproduced the warning on PHP 8.6.0: exif_read_data() raises "Invalid WebP file" and returns false for every WebP in the test data that has no Exif chunk. Rather than rely on the upstream fix landing, wp_read_image_metadata() now only passes a WebP to exif_read_data() when the file's VP8X header has the Exif flag set, through a small private helper, _wp_webp_has_exif_data(). That keeps 8.6.0 quiet too.

I've replaced test_exif_webp_is_not_read_before_php_8_6 with a test that checks the default array passed to wp_read_image_metadata_types, and added a test that four WebP files without Exif data raise no error. It uses a custom error handler, which still sees warnings silenced with @, and it fails without the guard.

I haven't looked at HEIF here, and I've left rotation out as you suggested.

The change and the test runs (PHP 8.4.25, 8.5.12 and 8.6.0) were done with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants