Skip to content

Modern Image Formats: let the browser encode client side uploads, and reflect it on the settings screen - #2663

Merged
adamsilverstein merged 20 commits into
WordPress:trunkfrom
adamsilverstein:add/mif-client-side-avif
Nov 17, 2026
Merged

adamsilverstein merged 20 commits into
WordPress:trunkfrom
adamsilverstein:add/mif-client-side-avif

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

This picks up the verification list from #2662. Short version: the plugin was telling the browser to produce WebP whenever the server could not encode AVIF, and the picture element assumed the server had made the sub-sizes. Both are addressed here, and I ran through each of the scenarios from the issue on WP 7.1 in the editor. There are Playground links below for anyone who wants to try it without a local setup.

Claude wrote the code and this write up, findings and all:

Description

Fixes #2662

With client side media processing (WordPress 7.1), the block editor uploads the original, then the browser generates the sub-sizes in whatever format the image_output_format REST field reports and sideloads them. That field comes from the image_editor_output_format filter, which the plugin only mapped to AVIF when the server's own image editor could encode AVIF. So on a host without AVIF support the browser was told to make WebP even though it could have made AVIF.

Three changes:

  • The REST requests of the client side flow are now recognized: attachment creation with generate_sub_sizes set to false, plus the sideload and finalize requests. This is exposed through a new webp_uploads_client_side_media_processing filter, which the plugin toggles for those requests. Within them, webp_uploads_get_upload_image_mime_transforms() no longer downgrades AVIF to WebP, and webp_uploads_filter_image_editor_output_format() maps the source type to the selected modern format without consulting the server's editor. That also applies when "Output fallback images" is on, since the server generates the original format sizes when the upload is finalized (the same path the plugin already uses for AVIF and WebP originals).
  • The picture element wrapper assumed the inner <img> was in the original format, which is true for server uploads but not for client side uploads, where the primary sub-size files are AVIF. The fallback <img> now gets its src and srcset pointed at the original format sources whenever the image is in a modern format.
  • The settings screen keeps AVIF and WebP selectable when core reports client side media processing is enabled. When the server cannot encode a format, the "support is not available" warning is replaced with a notice explaining that uploads are converted in the browser where it is able to:
Server encodes AVIF Client side media processing enabled What the user sees
yes either AVIF selectable, no notice
no yes AVIF selectable, "AVIF images are created in the browser", with a note that uploads which are not processed in the browser are converted to WebP on the server (or not converted, if the server lacks WebP too)
no no the existing "AVIF support is not available" notice

An earlier version of this PR mirrored the editor's runtime feature detection in an inline script to tell capable and incapable browsers apart. That was removed: it duplicated checks which would drift from core, and the notice wording now holds for either kind of browser. The screenshot below predates that change, the notice title now reads "AVIF images are created in the browser."

mif-settings-browser-avif-notice

Verification of the items in the issue

Tested on wp-env with WP 7.1, PHP 8.3, Imagick with AVIF and WebP, in Chrome 154 with cross-origin isolation active in the editor. A "no AVIF" server was simulated with a wp_image_editors filter swapping in an editor subclass that refuses image/avif. Uploading leaves.jpg (1080x720, under the big image threshold) from an image block:

  1. AVIF selected, server can encode it: the server converts the full size to AVIF during the create request as before, the browser produces the AVIF sub-sizes, and sources is recorded for every size.
  2. AVIF selected, server cannot encode it: before this change the browser was told to make WebP. Now it makes AVIF sub-sizes. The full size stays JPEG, since the server cannot convert it and the browser only sideloads a scaled full size when the image is over the threshold.
  3. "Output fallback images" on, server cannot encode AVIF: the browser makes the AVIF sub-sizes and the server generates the JPEG fallbacks for the supported sizes when the upload is finalized, eg. leaves-1024x683.avif plus leaves-1024x683-jpg.jpg. So the fallback sizes are still generated, by the server, and "Generate all fallback image sizes" governs them as usual.
  4. Picture element: <source type="image/avif"> carries the browser made AVIF files and the fallback <img> now carries the JPEG files. Before the picture element fix the <img> pointed at the AVIF files.
  5. sources metadata: the finalize request runs wp_generate_attachment_metadata, so webp_uploads_create_sources_property() records the browser made files and adds the server made fallbacks. Deleting the attachment removed every file, fallbacks included.

A Media Library upload on the simulated no-AVIF server still falls back to WebP on the server, so the non-editor path is unchanged.

Worth noting

  • The "discard larger generated images" rule works the same way as for server uploads: the fallback in the format of the upload is always kept, and a browser made AVIF or WebP which is larger than its fallback is discarded.
  • Related to the above, the AVIF sub-sizes produced by the browser were consistently larger than the equivalent JPEGs (251 KB vs 239 KB at 1024px). The plugin only pins WebP quality via wp_editor_set_quality; the AVIF quality the editor uses may be worth a look separately.
  • The settings screen keys off core's wp_is_client_side_media_processing_enabled(), so it stays server-only on 6.9 with the Gutenberg plugin. The REST detection works there too, since Gutenberg's controller extends the core one.

How has this been tested

  • New and existing PHPUnit tests for webp-uploads, single site and multisite, via npm run test-php:webp-uploads.
  • Manual testing in the block editor as described above, in both server modes and with fallback output on and off.
  • End-to-end tests which upload from an Image block in the editor, via npm run test-e2e:webp-uploads.
  • Settings screen notices covered by PHPUnit tests, with and without server support and client side media processing.

Testing instructions

Two Playground links, both WordPress 7.1 on PHP 8.3 with the plugin installed from this branch, logged in, landing on Settings > Media. Playground's PHP can encode AVIF on its own, so the first link covers the "server encodes AVIF" row of the table above. The second link adds a small mu-plugin that swaps in image editors refusing to encode AVIF, which covers the "server cannot encode AVIF" row. Use a recent Chrome, Firefox or Safari, since the client side pipeline needs WebAssembly and workers.

Steps, for either link:

  1. On the Media Settings landing page, under "Modern Image Formats", AVIF should be selected. On the first link there is no notice below the format select. On the second link the "AVIF images are created in the browser" notice appears, mentioning that uploads which are not processed in the browser are converted to WebP on the server instead.
  2. Go to Posts > Add New, add an Image block and upload a JPEG from your computer. Anything under 2560px wide keeps the full size untouched, which makes the next step easier to read.
  3. Once the upload finishes, open Media > Library and click the new image. On the first link the file name ends in .avif, since the server converted the full size. On the second link it is still the .jpg, and the sub-sizes are AVIF anyway.
  4. To see the sub-sizes, paste /wp-json/wp/v2/media?per_page=1&orderby=date&order=desc into the Playground address bar. Every entry in media_details.sizes should report image/avif with an .avif file, and each should carry a sources entry for image/avif. Before this change, on the second link, those came out as WebP.
  5. Fallback images and the picture element: back in Settings > Media, turn on "Output fallback images" and "Use Element", save, and upload another JPEG from an Image block. In the REST response each size should now list both image/avif and image/jpeg under sources, with JPEG files named like example-1024x640-jpg.jpg. Publish the post and view it: the <picture> has a <source type="image/avif"> pointing at the AVIF files, and the <img> src and srcset point at the JPEG files.
  6. On the second link, upload a JPEG through Media > Add New Media File. That path still goes through the server, so its sub-sizes should be WebP, unchanged from before.

Steps 1 through 4 were checked in Playground in Chrome for both links. Steps 5 and 6 come from the wp-env testing described above and were not re-run in Playground.

The mu-plugin used by the second link, for local testing
<?php
/**
 * Plugin Name: Simulate a server without AVIF support
 * Description: Testing aid for WordPress/performance#2663. Swaps in image editors that refuse to encode AVIF.
 */

add_filter(
	'wp_image_editors',
	static function ( array $editors ): array {
		if ( ! class_exists( 'No_AVIF_Image_Editor_GD' ) ) {
			class No_AVIF_Image_Editor_GD extends WP_Image_Editor_GD {
				public static function supports_mime_type( $mime_type ) {
					return 'image/avif' !== $mime_type && parent::supports_mime_type( $mime_type );
				}
			}
		}
		if ( ! class_exists( 'No_AVIF_Image_Editor_Imagick' ) ) {
			class No_AVIF_Image_Editor_Imagick extends WP_Image_Editor_Imagick {
				public static function supports_mime_type( $mime_type ) {
					return 'image/avif' !== $mime_type && parent::supports_mime_type( $mime_type );
				}
			}
		}
		return array( 'No_AVIF_Image_Editor_Imagick', 'No_AVIF_Image_Editor_GD' );
	}
);

Types of changes

  • Add webp_uploads_is_client_side_media_processing(), webp_uploads_is_client_side_media_processing_request(), webp_uploads_is_client_side_media_processing_enabled() and the webp_uploads_client_side_media_processing filter.
  • Track the client side flow around REST callbacks and use the selected modern format within it.
  • Point the picture element fallback <img> at the original format when the image is in a modern format.
  • Update the settings screen notices and options for client side media processing.

AI Use

Code and description both written with 🤖 Claude Code. I will review and test.

🤖 Generated with Claude Code

https://claude.ai/code/session_018vwYjSFt9ew4cJCFeSUJdV

When the block editor processes uploads client side (WordPress 7.1), the
browser encodes the sub-sizes in the format reported by the
image_output_format REST field. That field is computed from the
image_editor_output_format filter, which the plugin only mapped to AVIF
when the server's own image editor could encode AVIF, so a browser that
could produce AVIF was silently told to produce WebP instead.

Track the REST requests of the client side flow (attachment creation
with generate_sub_sizes disabled, sideload and finalize) and, within
them, keep the selected format in the mime transforms and map the
source type to it regardless of server support. Fallback output still
works because the server generates the original format sizes when the
upload is finalized.
With WordPress 7.1 the browser can encode AVIF and WebP for editor
uploads, so a format is no longer unavailable just because the server
cannot encode it. Keep both formats selectable when client side media
processing is enabled for the site, explain that the browser handles
editor uploads while other upload paths fall back, and let a runtime
feature check in the browser restore the server-only notices when the
browser cannot process media itself.
The picture element wrapper assumed the inner img is in the original
upload format, which holds when the server generates the sub-sizes and
adds the modern format as an additional source. With client side media
processing the browser generates the sub-sizes in the modern format and
the server adds the original format afterwards, so the fallback img
pointed at AVIF files. Point the src and srcset of the fallback img at
the original format sources whenever the image is in a modern format.
@adamsilverstein adamsilverstein added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Modern Image Formats Formerly WebP Uploads [slug: webp-uploads] labels Sep 3, 2026
@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.48872% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.84%. Comparing base (a30ea42) to head (b9290b5).

Files with missing lines Patch % Lines
plugins/webp-uploads/hooks.php 93.10% 4 Missing ⚠️
plugins/webp-uploads/helper.php 88.88% 1 Missing ⚠️
plugins/webp-uploads/settings.php 96.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2663      +/-   ##
==========================================
+ Coverage   70.41%   70.84%   +0.42%     
==========================================
  Files          91       91              
  Lines        7869     7967      +98     
==========================================
+ Hits         5541     5644     +103     
+ Misses       2328     2323       -5     
Flag Coverage Δ
multisite 70.84% <95.48%> (+0.42%) ⬆️
single 35.18% <44.36%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Sep 3, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @jonahwhite115.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: jonahwhite115.

Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>

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

@adamsilverstein

adamsilverstein commented Sep 3, 2026 •

Copy link
Copy Markdown
Member Author

This is a first pass at leveraging client side media for modern image support. The big caveat with 7.1 is the feature client side media is only available in the editor. For 7.2 I hope to land this in the media library as well, so maybe its worth waiting for 7.2 to try to land this feature?

Move the WP_REST_Request generic into a phpstan-param tag so the
FunctionComment sniff no longer misreads the type hint, and realign
the array double arrows and assignments phpcbf flagged in the tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CgmXFEKZyEoskNXDtv6fJy
@adamsilverstein adamsilverstein added this to the webp-uploads n.e.x.t milestone Sep 3, 2026
With the WordPress 7.1 stubs, PHPStan knows the finalize endpoint
always exists and reports the method_exists() guard as dead code.
Check the WordPress version instead, since the runtime check is still
needed on the WordPress 6.9 test matrix entry.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CgmXFEKZyEoskNXDtv6fJy
Comment thread plugins/webp-uploads/settings.php Outdated
<?php
webp_uploads_render_modern_image_support_unavailable_notice( true );
?>
<script>

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.

We should not be printing script tags manually as this violates CSP compat.

This should instead be:

	wp_print_inline_script_tag(
		<<<'JS'
		/* ... */
		JS
	);

See Core-59446 and WordPress/wordpress-develop#13319

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks!

I also wonder looking at this whether we could leverage the existing checks somehow (even though they don't typically run here) instead of duplicating them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Switched to wp_print_inline_script_tag() in 8fbb1da.

Claude checked whether the core detection could be reused here:

wp.uploadMedia.isClientSideMediaSupported() is available on this screen, but it bails on the missing SharedArrayBuffer because the settings page is not cross-origin isolated, so it would report unsupported in every browser. Until the package can skip that check, the mirrored detection seems the practical option.

Printing a script tag by hand bypasses the CSP nonce and other
attributes that wp_print_inline_script_tag() applies, as raised in
review. Match the pattern used by the Speculative Loading settings.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CgmXFEKZyEoskNXDtv6fJy
Comment thread plugins/webp-uploads/settings.php Outdated
Comment on lines +227 to +229
esc_html_e( 'Your server cannot generate AVIF images, but your browser can. Images uploaded in the editor are converted to AVIF in the browser. Images uploaded elsewhere, such as in the Media Library, are converted to WebP on the server instead.', 'webp-uploads' );
} else {
esc_html_e( 'Your server cannot generate AVIF images, but your browser can. Images uploaded in the editor are converted to AVIF in the browser. Images uploaded elsewhere, such as in the Media Library, are not converted.', 'webp-uploads' );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This same notice will be shown even if only transparency support is lacking for AVIF. Should we have a specific message for that, or include it in this same message?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess, its so specific to Imagick version i'm not sure we really need it?

Images uploaded elsewhere, such as in the Media Library, are not converted.

No longer true in 7.2

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Follow-up from Claude on the copy:

Reworded the browser notices in b47fa69 so they no longer say Media Library uploads are not converted; they now refer to uploads which are not processed in the browser. The transparency case still shares the existing notice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With "Output fallback images" enabled, JPEG uploads from the editor can lose their JPEG fallback sub-sizes. The browser generates AVIF sub-sizes first, and during finalization, webp_uploads_should_discard_additional_image_file() compares the generated JPEG fallback against the AVIF and discards the JPEG when it is larger.

As a result, browsers without AVIF support may have no usable fallback image.

The original-format fallback should not be discarded in this flow. If a size comparison is needed, the modern format should be discarded when it is larger, consistent with the server-side flow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point, I missed that. Will correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Claude made the fix, summary below:

Fixed in fb4fa50. The original format fallback is now always kept for browser generated sub-sizes, and the modern image is discarded instead when it is the larger one, matching the server side flow. Covered by two new tests in test-load.php.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still happens after fb4fa50. With "Output fallback images" enabled and AVIF selected, a JPEG or PNG uploaded from the editor still loses the JPEG fallback for each sub-size where the browser generated AVIF is smaller.

The cause is in the upload request, before finalize:

  1. The browser uploads the original with generate_sub_sizes: false and does not send convert_format.
  2. webp_uploads_is_client_side_media_processing_request() treats this create_item request as client side, so webp_uploads_filter_image_editor_output_format() maps image/jpeg to image/avif, also with fallback enabled.
  3. wp_create_image_subsizes() sees that the output format differs from the upload, so the server converts the full size image to AVIF and makes it the attached file. The uploaded JPEG becomes original_image.
  4. During finalize, webp_uploads_create_sources_property() gets image/avif as $mime_type from the attached file. For the JPEG fallback of a sub-size, $mime === $mime_type is false, so the new branch is skipped and the old elseif discards the JPEG when it is larger than the AVIF.

Replaying the upload, sideload and finalize requests on WordPress 7.1.4 with leaves.jpg and a 3,698 byte AVIF medium size (the JPEG fallback is 22,541 bytes):

Upload request Attached file after upload Medium sources after finalize
As the browser sends it leaves.avif (original_image: leaves.jpg) image/avif only
Same, with convert_format: false leaves.jpg image/avif and image/jpeg

The same happens with dice.png.

The new tests pass because they build the attachment with create_upload_object(), which is not a client side request, so the attached file stays a JPEG and the new branch runs.

The same image_editor_output_format filter also sets image_output_format in the upload response, which is what tells the browser to encode the sub-sizes as AVIF. So the mapping on this request does two things: it tells the browser the format, and it makes the server convert the full size image.

🤖 Generated with Claude Code

cc: @adamsilverstein

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, let me test this further @b1ink0 - thanks for the feedback!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the detailed breakdown @b1ink0, you were right - the earlier fix never ran in the real flow because the server had already converted the upload. Can you please re-test when you have a chance?

Claude reproduced this in the editor and reworked the fix:

Confirmed, and reproduced with a new Playwright spec that uploads from an Image block. Fixed in 88dd7b9, with two follow ups for the picture element in a697b15 and 10957f6.

As suggested by the last point in the review, the two jobs of the mapping are now separated. With fallback output enabled, webp_uploads_filter_image_editor_output_format() withholds the mapping while the server processes the upload in the create_item request, and applies it again once wp_generate_attachment_metadata has run. So the upload stays the attached file, as it does for a server side upload, and image_output_format in the response still tells the browser to encode AVIF. convert_format is left alone, since forcing it off would also stop the HEIC to JPEG conversion.

Reproducing it in a browser turned up three related gaps, fixed in the same push:

  • PNG uploads fell back to JPEG, since the fallback format was derived from the converted AVIF. They now fall back to PNG.
  • For an image over the big image size threshold the browser sideloads the scaled full size as AVIF, so the attached file is AVIF again and the same discard happened. The fallback format is now taken from original_image in that case, and the scaled AVIF replaces the one the server generated from the upload, which was otherwise left behind on delete.
  • The picture element only fixed the fallback <img> srcset when its src was AVIF. With a JPEG or PNG full size and AVIF sub-sizes, the fallback srcset still listed AVIF files. For a scaled upload no picture element was output at all.

Results in Chrome 157 on WordPress 7.1.4, AVIF selected, fallback and picture element on:

Upload Attached file Sub-size sources Fallback <img>
car.jpeg car.jpeg image/avif and image/jpeg for every size JPEG only
dice.png dice.png image/avif and image/png for every size PNG only
paint.jpeg scaled up to 2133x3200 -scaled.avif, original_image is the JPEG image/jpeg for every size (the AVIFs were larger, so discarded) JPEG only

Tests:

  • test-load.php now replays the upload, sideload and finalize requests through the REST server instead of create_upload_object(). Those tests fail without the fix.
  • New e2e spec at plugins/webp-uploads/tests/e2e/specs/client-side-media-processing.spec.js, covering JPEG, PNG, a 3000x2000 image and the no fallback case, including the published picture element markup.

One limitation left as is: for a scaled upload the full size falls back to the original upload rather than a scaled JPEG, so that one candidate is larger than its width descriptor says. Generating a scaled JPEG there needs a change to how sources are removed on delete, which seemed better as a follow up.

adamsilverstein and others added 2 commits November 5, 2026 21:29
When the browser generates the sub-sizes in the modern format, the
fallback in the original format was compared against the modern image
and discarded when larger, leaving browsers without modern format
support with no usable image. Keep the fallback, and as in the server
side flow, discard the modern image instead when it is the larger one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjRmoh5zeRajtJu5rom
Since WordPress 7.2 the Media Library also processes uploads client
side, so describe the fallback in terms of uploads the browser does not
process rather than naming the editor and the Media Library.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjRmoh5zeRajtJu5rom
@b1ink0

b1ink0 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

As PR #2750 is merged can you please resolve merge conflicts.

…avif

# Conflicts:
#	plugins/webp-uploads/settings.php
adamsilverstein and others added 6 commits November 7, 2026 19:56
With fallback output enabled, the request creating the attachment mapped
the upload to the modern format, so the server converted the full size
image and made it the attached file. The fallbacks were then derived
from the modern format: a larger JPEG was discarded, and a PNG fell back
to JPEG.

Withhold the mapping while the server processes the upload, so the
upload stays the attached file as it does for a server side upload, and
still report it in the response for the browser to encode the sub-sizes.

For an upload over the big image size threshold the browser supplies the
scaled full size image in the modern format. Fall back to the format of
the original image in that case, and let the scaled image replace the
one the server generated from the upload.

Cover the flow by replaying the upload, sideload and finalize requests.

Claude-Session: https://claude.ai/code/session_018vwYjVaaNrBvxVmEH6cjHe
Upload images from an Image block in a real browser, so that the format
of the attached file and the fallbacks of every sub-size are verified
against what the editor actually sends.

Claude-Session: https://claude.ai/code/session_018vwYjVaaNrBvxVmEH6cjHe
The fallback IMG only had its srcset pointed at the original format when
its src was in a modern format. For a client side upload the full size
image is in the original format while the sub-sizes are not, so the
fallback srcset still listed modern images.

For an upload over the big image size threshold the attached file is the
scaled image in the modern format, which was taken as the format to fall
back to, so no picture element was output at all. Fall back to the
format of the upload kept as the original image instead.

Claude-Session: https://claude.ai/code/session_018vwYjVaaNrBvxVmEH6cjHe
Publish the post with the uploaded image and check that the fallback IMG
of the picture element is free of AVIF images.

Claude-Session: https://claude.ai/code/session_018vwYjVaaNrBvxVmEH6cjHe
A single modern image does not make for a picture source, which is the
case when only the scaled full size image supplied by the browser is in
the modern format. The image was then left as is, listing that modern
image in its srcset with nothing to fall back to.

Claude-Session: https://claude.ai/code/session_018vwYjVaaNrBvxVmEH6cjHe

@b1ink0 b1ink0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have tested the behavior, and it is now working as expected.

Below is the feedback from Claude that I requested because the recent changes have increased the PR to over 1,800 lines. The feedback is related to duplicate or dead code. It can be ignored if needed.

Comment thread plugins/webp-uploads/hooks.php Outdated
Comment on lines +106 to +109
$superseded = is_string( $source_file ) && wp_basename( $file ) !== $source_file && webp_uploads_is_client_side_media_processing();
if ( $superseded && ( $metadata['original_image'] ?? null ) !== $source_file ) {
wp_delete_file_from_directory( path_join( dirname( $file ), $source_file ), dirname( $file ) );
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For a client side upload, the request that creates the attachment already encodes a full size modern image from the upload (for example leaves-jpg.avif). For an upload over the big image size threshold, the browser then sideloads its own -scaled image, and these lines delete the server one during finalize. With a 3000x2000 JPEG, AVIF selected and fallback on, the server encodes a 1.6 MB AVIF in the upload request and deletes it a few seconds later.

The full size loop could be skipped in the upload request and left to finalize. wp_generate_attachment_metadata passes 'create' as the third argument in the upload request and 'update' in finalize, so after sources[ $mime_type ] is recorded (before line 119):

if ( 'create' === $context && webp_uploads_is_client_side_media_processing() ) {
	return $metadata;
}

with string $context = '' added to the function at line 59 and the hook at line 303 changed to accept 3 arguments.

The server then never writes a full size modern file that the scaled image replaces, so lines 107-109 can be removed. The $superseded replacement at line 111 is still needed: for an upload already in the selected format (for example a WebP over the threshold with WebP selected and fallback on), sources['image/webp'] points at the upload until finalize, and has to point at the scaled image after it.

I tried this with lines 107-109 removed. The webp-uploads PHPUnit suite passes once the scaled test stops expecting a server WebP after the upload (lines 1446-1447 and 1462 in tests/test-load.php). The full size WebP is then created during finalize. I haven't run the end-to-end spec with it.

If the delete is kept instead, the original_image check on line 107 protects the upload in the same format case above: without it, finalize deletes the uploaded WebP (original_image) from disk. No test covers that case. A test like test_it_should_keep_the_fallbacks_of_a_scaled_client_side_upload with a WebP upload over the threshold would.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 2c54439, with a test for the WebP upload over the threshold.

Comment thread plugins/webp-uploads/hooks.php Resolved
Comment thread plugins/webp-uploads/hooks.php Resolved
Comment thread plugins/webp-uploads/hooks.php Resolved
* @covers ::webp_uploads_start_client_side_media_processing_request
* @covers ::webp_uploads_end_client_side_media_processing_request
*/
public function test_it_should_enable_client_side_media_processing_while_dispatching_finalize_request(): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This overlaps with test_it_should_track_client_side_media_processing_around_rest_callbacks() (line 1188), and the upload, sideload and finalize tests from line 1393 only pass when the context is set during those requests. The part they don't check is that the context is cleared after a request, and one of these two tests is enough for that.

Also, line 1233 skips with version_compare( get_bloginfo( 'version' ), '7.1', '<' ), while enable_client_side_media_processing() at line 1296 skips with function_exists( 'wp_is_client_side_media_processing_enabled' ) for the same condition.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the function_exists check should be preferred over a version check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed both in 2c54439, details in #2663 (comment)

The inline script mirrored the feature detection of the editor's
upload-media package, which would drift from core over time. The notices
now say that images are converted in the browser where it is able to,
which holds for any browser.

Claude-Session: https://claude.ai/code/session_018vwYjSFt9ew4cJCFeSUJdV
Leave the full size modern image to the finalize request, so the server
no longer encodes one in the upload request which the scaled image from
the browser then replaces. Detect the requests in the one hook which
needs to, share the lookup of the original image format with the picture
element, and drop two conditions which could not be reached.

Remove the tests whose behavior is covered by the tests replaying the
upload, sideload and finalize requests.

Claude-Session: https://claude.ai/code/session_018vwYjSFt9ew4cJCFeSUJdV
The remaining tests cover the scaled upload, which exercises the same
fallback src and srcset replacement.

Claude-Session: https://claude.ai/code/session_018vwYjSFt9ew4cJCFeSUJdV
@adamsilverstein

Copy link
Copy Markdown
Member Author

Thanks for the thorough review @b1ink0, and for trying the upload request change before suggesting it. I took all of it, and went a bit further to get the size of this down - it is now around 1,300 lines, from 1,800. Whenever you have a chance, can you give it another look?

Claude worked through the review and trimmed the PR, summary below:

All ten review comments are addressed in 2c54439 and f8185d3. Where the change differs from what was suggested:

  • hooks.php full size loop: skipped in the upload request as suggested, so the server no longer encodes a full size image which the scaled one replaces. The replacement of sources for an upload already in the selected format is kept, and now has a test with a WebP upload over the threshold.
  • Request detection: webp_uploads_is_client_side_media_processing_request() is gone. The handler is only looked at in webp_uploads_start_client_side_media_processing_request(), which also uses the generate_sub_sizes check directly.
  • original_image format: both callers use a new webp_uploads_get_original_image_mime_type(). Each keeps its own check of whether that format is usable as the fallback, since one works from the transforms and the other from the recorded sources.
  • Context tests: both were removed. The tests replaying the upload, sideload and finalize requests now assert that the context is cleared after the request.
  • Settings test with the host dependent expectations: removed along with the code it covered, see below.

Two further simplifications which were not part of the review:

  • The inline script on the settings screen is removed (f8185d3). It mirrored the feature detection of the upload-media package, which would drift from core. The notice now reads "AVIF images are created in the browser" and says uploads are converted in the browser where it is able to, which holds for any browser. The trade off: on a server without AVIF, a browser which cannot process images still sees AVIF as selectable instead of the "not available" warning.
  • Two picture element tests which built by hand the same metadata the end-to-end spec produces through the editor are removed (7a4d1a3).

The webp-uploads PHPUnit suite and the end-to-end spec both pass on WordPress 7.1.4 with these changes. Trunk is merged in as well.

@b1ink0
b1ink0 self-requested a review November 17, 2026 06:18

@b1ink0 b1ink0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

Excited to get this released before 7.2 release.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical MIME-selection bug causes AVIF uploads to be reported as JPEG conversions, and the no-AVIF integration path remains untested.

2 open issues
What changed in this PR

This PR updates Modern Image Formats to support WordPress client-side media processing, allowing browsers to generate AVIF/WebP sub-sizes while preserving server-generated fallbacks and correct picture output.

Changes:

  • Detects client-side upload REST requests and preserves the configured modern format independently of server encoder support.
  • Updates metadata, fallback sources, picture-element output, and settings notices for browser-generated images.
  • Adds PHPUnit and Playwright coverage plus E2E project and CI configuration.
File Description
tools/​e2e/​playwright.config.ts Registers the client-side media processing Playwright project.
plugins/​webp-uploads/​tests/​test-settings.php Tests settings behavior and notices with and without client-side media processing.
plugins/​webp-uploads/​tests/​test-picture-element.php Tests fallback picture markup for modern-format uploads.
plugins/​webp-uploads/​tests/​test-load.php Tests REST upload lifecycle, metadata, fallback, and source handling for client-side uploads.
plugins/​webp-uploads/​tests/​test-helper.php Tests format transforms when server support is unavailable during client-side processing.
plugins/​webp-uploads/​tests/​e2e/​specs/​client-side-media-processing.spec.js Adds end-to-end coverage for editor uploads and fallback images.
plugins/​webp-uploads/​settings.php Keeps modern formats selectable and displays client-side processing notices.
plugins/​webp-uploads/​picture-element.php Points fallback image sources at the original upload format when appropriate.
plugins/​webp-uploads/​hooks.php Tracks client-side REST requests and records browser-generated and fallback sources.
plugins/​webp-uploads/​helper.php Adds client-side processing detection and modern-format transform helpers.
package.json Adds a dedicated WebP Uploads E2E test command.
.github/​workflows/​e2e-test.yml Runs E2E checks when relevant WebP Uploads files change.

Review effort: Lite

Comment thread plugins/webp-uploads/hooks.php Resolved
Comment thread plugins/webp-uploads/hooks.php Resolved
The point of letting the browser encode is that AVIF works on servers which cannot encode it, but the request replay tests only ran with the host's real image editor, so that combination was only checked through direct calls to the output format filter. The new test swaps in an editor without modern format support around the upload and finalize requests and checks that the browser is told AVIF, the upload is kept, and the browser's AVIF survives next to the server's JPEG fallback. Reverting the client side check in webp_uploads_get_upload_image_mime_transforms() makes it fail.

The sideload helper now takes the mime type to encode, defaulting to WebP so the existing tests are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjShn8NhWrW4DKd28oz
@adamsilverstein

Copy link
Copy Markdown
Member Author

Manual testing runs orchestrated by Claude:

performance-2663-jpeg-only-server-picture-fallback.mp4
performance-2663-avif-without-server-avif.mp4

@adamsilverstein

Copy link
Copy Markdown
Member Author

This worked well in my manual testing as well.

@adamsilverstein
adamsilverstein merged commit 2dfe54c into WordPress:trunk Nov 17, 2026
33 of 36 checks passed
@b1ink0

b1ink0 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

For reference, while testing this I found a core issue with client side uploads when the output format is filtered, with or without this plugin. The server converts the main file of the upload, and the browser's own scaled or rotated image then replaces it, so original_image points at the converted copy and the uploaded file stays on disk after "Delete permanently". With this plugin it happens when fallback output is off, since the upload is converted then. It reproduces with any image_editor_output_format filter and no plugins active: https://core.trac.wordpress.org/ticket/66763

@adamsilverstein

Copy link
Copy Markdown
Member Author

For reference, while testing this I found a core issue with client side uploads when the output format is filtered, with or without this plugin. The server converts the main file of the upload, and the browser's own scaled or rotated image then replaces it, so original_image points at the converted copy and the uploaded file stays on disk after "Delete permanently". With this plugin it happens when fallback output is off, since the upload is converted then. It reproduces with any image_editor_output_format filter and no plugins active: https://core.trac.wordpress.org/ticket/66763

Thanks for opening the ticket @b1ink0 - that sounds like a bug, the server shouldn't convert the uploaded image when client side processing is handling the image.

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

Labels

[Plugin] Modern Image Formats Formerly WebP Uploads [slug: webp-uploads] [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modern Image Formats: verify AVIF output with client side media uploads, and reflect it on the settings screen

4 participants