Repository navigation
Modern Image Formats: let the browser encode client side uploads, and reflect it on the settings screen - #2663
Conversation
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.
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Unlinked AccountsThe 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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
|
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
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
| <?php | ||
| webp_uploads_render_modern_image_support_unavailable_notice( true ); | ||
| ?> | ||
| <script> |
There was a problem hiding this comment.
We should not be printing script tags manually as this violates CSP compat.
This should instead be:
wp_print_inline_script_tag(
<<<'JS'
/* ... */
JS
);There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 missingSharedArrayBufferbecause 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
| 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' ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point, I missed that. Will correct.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- The browser uploads the original with
generate_sub_sizes: falseand does not sendconvert_format. webp_uploads_is_client_side_media_processing_request()treats thiscreate_itemrequest as client side, sowebp_uploads_filter_image_editor_output_format()mapsimage/jpegtoimage/avif, also with fallback enabled.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 becomesoriginal_image.- During finalize,
webp_uploads_create_sources_property()getsimage/avifas$mime_typefrom the attached file. For the JPEG fallback of a sub-size,$mime === $mime_typeis false, so the new branch is skipped and the oldelseifdiscards 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
There was a problem hiding this comment.
Hmm, let me test this further @b1ink0 - thanks for the feedback!
There was a problem hiding this comment.
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 thecreate_itemrequest, and applies it again oncewp_generate_attachment_metadatahas run. So the upload stays the attached file, as it does for a server side upload, andimage_output_formatin the response still tells the browser to encode AVIF.convert_formatis 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_imagein 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 itssrcwas 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.jpegcar.jpegimage/avifandimage/jpegfor every sizeJPEG only dice.pngdice.pngimage/avifandimage/pngfor every sizePNG only paint.jpegscaled up to 2133x3200-scaled.avif,original_imageis the JPEGimage/jpegfor every size (the AVIFs were larger, so discarded)JPEG only Tests:
test-load.phpnow replays the upload, sideload and finalize requests through the REST server instead ofcreate_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.
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
|
As PR #2750 is merged can you please resolve merge conflicts. |
…avif # Conflicts: # plugins/webp-uploads/settings.php
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
left a comment
There was a problem hiding this comment.
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.
| $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 ) ); | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Done in 2c54439, with a test for the WebP upload over the threshold.
| * @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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
the function_exists check should be preferred over a version check.
There was a problem hiding this comment.
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
|
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:
|
b1ink0
left a comment
There was a problem hiding this comment.
LGTM!
Excited to get this released before 7.2 release.
There was a problem hiding this comment.
🟡 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
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
|
Manual testing runs orchestrated by Claude: performance-2663-jpeg-only-server-picture-fallback.mp4performance-2663-avif-without-server-avif.mp4 |
|
This worked well in my manual testing as well. |
|
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 |
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. |


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