Skip to content

Upload Media: Convert the main file in the browser for client side uploads - #87135

Open
b1ink0 wants to merge 6 commits into
WordPress:trunkfrom
b1ink0:fix/upload-media-main-file-conversion
Open

b1ink0 wants to merge 6 commits into
WordPress:trunkfrom
b1ink0:fix/upload-media-main-file-conversion

Conversation

@b1ink0

@b1ink0 b1ink0 commented Nov 20, 2026 •

Copy link
Copy Markdown

What?

Follow up to #75793 and #80295.

Makes the browser the only place that writes the main file of a client side upload. The server no longer converts it when the output format is filtered.

This should be merged after #83785, and rebased on it.

Why?

Core ticket: https://core.trac.wordpress.org/ticket/66763

With an image_editor_output_format filter (for example JPEG to WebP), two places write the main file of a client side upload. The server converts photo.jpg to photo.webp in the upload request, then the browser's scaled sideload (big images) or rotated original sideload (EXIF rotated images) replaces it again. The sideload records the file it replaces, photo.webp, as original_image. Nothing points at photo.jpg anymore, so it stays on disk after "Delete permanently".

The server converts the main file because of #75793. At the time, an original sideload only set original_image and didn't replace the main file, so the browser couldn't supply a converted main file. #80295 changed that, so the workaround is no longer needed.

How?

  • create_item() turns off main file conversion for uploads with generate_sub_sizes set to false, once the upload is stored. The upload's name still reserves the names of the converted files. If another attachment already has photo.webp, the upload is saved as photo-1.jpg and its converted copy as photo-1.webp.
  • When the image isn't scaled and a format conversion applies, generateThumbnails() sideloads a full size converted copy as the original, rotated if the EXIF orientation needs it. The rotated original is only sent when there's no conversion. So at most one sideload replaces the main file.
  • This also runs for images with no missing sub sizes, so very small images are converted too.
  • When a scaled or original sideload replaces the main file with an image in another format, the sideload also updates the attachment's MIME type, so a photo.webp main file is no longer reported as image/jpeg (#53665). The e2e tests for format conversion now expect the converted MIME type.

Core counterpart: WordPress/wordpress-develop#16119

Testing Instructions

  1. Add a must-use plugin that converts JPEG to WebP:
    <?php
    add_filter(
    	'image_editor_output_format',
    	function ( $formats ) {
    		$formats['image/jpeg'] = 'image/webp';
    		return $formats;
    	}
    );
  2. Open a post and upload a JPEG smaller than 2560 pixels to an Image block.
  3. Verify that the uploads folder has photo.jpg, photo.webp and the WebP sub sizes, and that the Media Library shows photo.webp as the file and image/webp as the file type.
  4. Delete the image permanently from the Media Library.
  5. Verify that no photo files are left in the uploads folder.
  6. Repeat with a JPEG larger than 2560 pixels. Verify that the uploads folder has photo.jpg and photo-scaled.webp and no photo.webp, and that nothing is left after a permanent delete.
  7. Repeat with a photo taken in portrait orientation on a phone (EXIF orientation 6), smaller than 2560 pixels. Verify that the main file is photo.webp, shown upright, and that nothing is left after a permanent delete.

Testing Instructions for Keyboard

No UI changes.

Screenshots or screencast

Big image before:

trunk-big.mp4

Big image after:

after-big.mp4

Rotated image before:

trunk-phone.mp4

Rotated image after:

after-phone.mp4

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Tracing the upload flow, a script that replays the client's REST requests, and drafts of the fix. I reviewed, edited and tested the final changes.

🤖 Generated with Claude Code

…loads

With an `image_editor_output_format` filter, the server converted the main file of client side uploads, and the browser then replaced it with its own scaled or rotated image. The sideload recorded the server's converted copy as `original_image`, so the upload stayed on disk after a permanent delete.

The server now leaves the main file unconverted. When the image isn't scaled, the browser sideloads a full size converted copy as the `original`, rotated if needed, so only one sideload replaces the main file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @b1ink0! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

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

Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@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.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@b1ink0
b1ink0 marked this pull request as ready for review November 20, 2026 07:20
@b1ink0
b1ink0 requested a review from spacedmonkey as a code owner November 20, 2026 07:20
@mikachan mikachan added [Feature] Client Side Media Media processing in the browser with WASM [Type] Enhancement A suggestion for improvement. labels Nov 20, 2026
@adamsilverstein
adamsilverstein requested review from adamsilverstein and andrewserong and removed request for spacedmonkey November 20, 2026 19:02
@adamsilverstein

Copy link
Copy Markdown
Member

Thanks for working on this @b1ink0, moving the main file conversion to the browser makes sense to me and the add_attachment trick for keeping the reserved file names is a nice touch.

I had Claude review and test this one locally, here is what it found:

The approach holds up for the vips path, but testing turned up one regression on the HEIC canvas fallback path (browsers without SharedArrayBuffer, eg. Safari without cross-origin isolation).

On that path the upload is sent with generate_sub_sizes: true, so the server still converts the main file and creates the sub sizes. The response comes back with missing_image_sizes: [], and image_output_format is still set because the post mime type stays image/jpeg after the server conversion. The old length > 0 check skipped the block; the new attachment.missing_image_sizes check is true for an empty array, so the browser sideloads a second converted original on top of the file the server already replaced.

Steps used to reproduce, in wp-env with a JPEG to WebP image_editor_output_format filter:

  1. Open the post editor in Chrome with SharedArrayBuffer removed before page load (delete window.SharedArrayBuffer in an init script), so the editor falls back to HEIC canvas processing.
  2. Upload test/e2e/assets/1024x768_e2e_test_image_exif_camera.heic to an Image block.
  3. Check the uploads folder and the attachment metadata, then delete the attachment permanently.

Results:

trunk this PR
Sideloads source_original source_original and original
original_image …camera.jpg …camera.webp (the server's conversion)
Left on disk after delete nothing …camera.jpg

So the uploaded JPEG winds up orphaned again, the same problem this PR fixes for the vips path. Limiting the main file sideload to uploads that were sent with generate_sub_sizes: false should cover it. Note: this was tested in Chrome with SharedArrayBuffer removed, not in Safari itself, and a HEIC over 2560 pixels was not tested (by the same logic it would likely queue a second scaled sideload).

A few smaller things from reading the diff:

  • There are no new unit tests in packages/upload-media/src/store/test/private-actions.ts. Cases worth covering: converted original sideload for a small image, no original sideload when the image is scaled, rotated plus converted sends a single sideload, and the HEIC fallback case above.
  • The PHP test only flips one assertion. A test for the name reservation (photo-1.jpg when another attachment already has photo.webp) would help.
  • The comment in create_item() says what happens but not why the filter is added on add_attachment rather than up front. The reason (so wp_unique_filename() still reserves the converted names) is only in the PR description.
  • The rotated original sideload moved inside the missing_image_sizes block, so it is skipped if that field is ever missing from the response.
  • This PR already skips both main file sideloads when needsScaling is true, so it fixes the Client-side media: original_image names the rotated copy, not the upload, for a large photo with EXIF rotation #83757 symptom on its own. What Media: Preserve original_image metadata for large photos with EXIF rotation #83785 adds is skipping the unneeded full size rotation.

Does the generate_sub_sizes check sound right to you, or do you see a better place to guard this?

…he sub-sizes.

The HEIC canvas fallback uploads with `generate_sub_sizes` set to true, so the server already scales and converts the main file. The client then sideloaded a second `scaled` or `original` file over it, and the uploaded file was left on disk after a permanent delete. The main file sideloads now only run for uploads sent with `generate_sub_sizes` set to false.

Adds unit tests for the main file sideloads, a test for the converted file name reservation, and explains in `create_item()` why the filter is added on `add_attachment`.

Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@b1ink0

b1ink0 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks for testing this, @adamsilverstein. The HEIC fallback regression is real.

Does the generate_sub_sizes check sound right to you, or do you see a better place to guard this?

Yes. generate_sub_sizes: false is the only case where the server leaves the main file to the client, so both main file sideloads (scaled and original) are now limited to it. The sub-size handling is unchanged. With the guard, both HEIC cases leave nothing on disk after a permanent delete, the same as trunk. A small JPEG, a big JPEG, an EXIF-rotated JPEG, and a HEIC on the vips path still keep the upload as original_image and leave nothing after deletion.

For the other points:

  • Added unit tests for a converted original sideload for a small image, no original sideload when the image is scaled, a single sideload for a rotated and converted image, and no main file sideload when the server generates the sub-sizes (the HEIC fallback). The last one fails if either guard is removed.
  • Added a PHP test for the name reservation: with photo.webp already in the uploads folder, photo.jpg is stored as photo-1.jpg. It fails if the filter is added before the upload instead of on add_attachment.
  • The comment in create_item() now explains why the filter is added on add_attachment.
  • The rotated original sideload is inside the missing_image_sizes block, but the upload response always includes that field because the controller sets it whenever it is requested source. So I kept it there, with the rest of the main file handling.
  • Agreed on Media: Preserve original_image metadata for large photos with EXIF rotation #83785.

b1ink0 and others added 3 commits November 27, 2026 22:07

This branch has not been deployed

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

Labels

[Feature] Client Side Media Media processing in the browser with WASM First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants