Repository navigation
Client-side media: Process image edits (crop, rotate, flip) with wasm-vips - #82362
adamsilverstein wants to merge 30 commits into
Conversation
Applies the REST /edit endpoint's modifiers with libvips so image edits can run in the browser. EXIF orientation is applied first, matching the frame the user previewed, and an UltraHDR gain map is transformed in step with the base image instead of being dropped.
Queues an image edit ahead of the regular upload pipeline so the edited file is uploaded as a new attachment with client-side sub-sizes. A failing edit is reported with IMAGE_EDIT_ERROR before anything reaches the server, so callers can fall back to the /edit endpoint.
Fetch the original file, apply crop, rotate and flip with vips and upload the result through the upload queue, falling back to the server /edit endpoint when client-side processing is unavailable. Keeps HDR gain maps that the server-side editors strip (#82295).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review. 📝 WalkthroughWalkthroughThis change adds client-side crop, rotate, and flip processing through wasm-vips. Edited files use the upload queue, preserve UltraHDR gain maps, create linked attachments, and fall back to the REST edit endpoint when client-side processing is unavailable. ChangesClient-side image editing
Estimated code review effort: 5 (Critical) | ~90 minutes Merge Risk: ⚪ Minimal · up to Edited-image queue work is covered by the reviewed selector tests, with no outstanding issue identified for this change. Sequence Diagram(s)sequenceDiagram
participant MediaEditor
participant EditImageClientSide
participant UploadMedia
participant VipsWorker
participant AttachmentsController
MediaEditor->>EditImageClientSide: Submit original file and modifiers
EditImageClientSide->>UploadMedia: Enqueue edited attachment
UploadMedia->>VipsWorker: Process image modifiers
VipsWorker-->>UploadMedia: Return edited file and dimensions
UploadMedia->>AttachmentsController: Create attachment with parent_image
AttachmentsController-->>UploadMedia: Return finalized attachment
UploadMedia-->>MediaEditor: Return attachment ID
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Full details: Linked Issues checkExplanation The changes satisfy the linked objectives [ Full details: Out of Scope Changes checkExplanation The implementation, metadata handling, documentation, configuration, tests, and changelog entries directly support the linked image-editing and gain-map objectives. No unrelated code changes are evident. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Some tools did not complete. Review the errors below. 🔧 ESLint
packages/upload-media/src/store/actions.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. packages/upload-media/src/store/private-actions.tsESLint skipped: the matched ESLint configuration already failed (missing-dependency). packages/upload-media/src/store/private-selectors.tsESLint skipped: the matched ESLint configuration already failed (missing-dependency).
Comment |
🤖 PR meta 🤖🎉 PropsUnlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @t-wright. 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:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +2.65 kB (+0.03%) Total Size: 8.69 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. does not select blocks in the canvas preview when editing from the sidebar (Site Editor v2) in
|
Add a parent_image parameter to the media create endpoint so an image edited in the browser relates to its source the way the /edit endpoint's new attachment does: the parent_image metadata entry, the source's EXIF fields the new file lacks, and an upright orientation. The media editor passes the edited attachment's ID along with the upload.
Rotate and crop an UltraHDR image through the media editor modal and check that no /edit request is made, that the gain map survives on the full-size file and its sub-sizes, and that the new attachment carries the source's details and parent_image relation.
|
Thanks so much for getting this started! Just cross-linking this to the WIP PR to track the original attachment id for crops over in #81803 as it's an overlapping area that @ramonjd has been looking into. It seems for both of these features (restore original is the other feature) we'll need to track the relationship of the new attachment back to the original attachment the crop was performed from. So, hopefully we can find a good shape that serves both purposes. Excited to see the client-side media stuff roll out to the media editor! 🎉 |
The edit step replaced the queue item's file but left sourceFile, which generateThumbnails reads, pointing at the un-edited original. Sub-sizes of an edited image were therefore made from the source attachment, one edit behind the full-size file. Replace sourceFile with the edited file too. See #82295 (comment)
… image Compare each sub-size's pixels against the region of the edited full-size image it is resized or centre-cropped from, so a sub-size made from the source attachment fails even when its dimensions match (a flip). Cover rotate, flip, crop and a second edit of an edited image, each checking the gain map on every size.
|
Greg's Playground testing over on #82295 (comment) turned up a real bug: the full size was edited correctly but every sub-size was one edit behind. Fixed in 2afe630, with e2e coverage in 6b333dd. Claude tracked this one down, here is the write up:
@gregbenz whenever you have a chance, can you please re-test in Playground once the build for the new commits lands? https://playground.wordpress.net/gutenberg.html?pr=82362 |
|
@adamsilverstein Just tested https://playground.wordpress.net/gutenberg.html?pr=82362 Test #1: Dragged 2 JPG gain maps at same time into post. Immediately selected and changed resolution to "thumbnail" on one and then "medium" to the other. The preview of the assets stayed the same, but the small size showed low quality - suggesting that it was loading the smaller assets but scaling all to the same size on screen. Test #2: Selected one to edit with crop, flip, and rotate 90 degrees. Result was still HDR at all sizes. However, I saw the wrong part of the image, not the same area I was viewing while cropping. I assume this is probably a manifestation of the first finding. Test #3: deleted those images and uploaded a single (one of the same previous, just not two at same time). Changing resolution worked. Crop, flip, rotate worked and immediately showed the correct size / result. Test #4: repeated #1 and saw same results. Uploading two images at the same time seems to be a problem. |
…ate test Saving through the media editor modal now applies the edit and generates sub-sizes in the browser when client-side media processing is available, so closing the modal takes longer than the default expectation timeout.
The media editor now dispatches the upload queue's addEditedImage action, which pulls the upload-media store and its lock-unlock into the graph.
Pass the metadata recorded for a client-side edit through the same filter core fires for the edit endpoint's new attachment, so hooks such as the original attachment lineage see edits made in the browser too.
|
@adamsilverstein FYI that I am proposing upstream improvements in wasm-vips so that flips/rotations work by default. I believe moving upstream has the most benefit so that other users of that library benefit (and I could not find any examples of downstream use that would break, which makes sense for such a transform with a new capability). If that PR is accepted by wasm-vips, I'll provide an update here. At that time, I would be happy to help suggest changes or create an updated PR for WordPress (doing so will simplify this PR and will improve performance by avoiding a safe but wasteful extra flip/rotation calculation). |
Great idea, thanks @gregbenz! |
…image-edits # Conflicts: # packages/upload-media/src/store/test/vips.jsdom.test.ts
|
@adamsilverstein Quick update: wasm-vips declined the PR with their intention being to keep it a thin wrapper around libvips (ie wrong level to target the change, I'm evaluating). |
…image-edits # Conflicts: # packages/media-editor/CHANGELOG.md # packages/upload-media/CHANGELOG.md # packages/vips/CHANGELOG.md
The edit operation ran ahead of prepareItem's large-image gate, so an image too big for the wasm-vips memory cap was downloaded and decoded before anything caught it. Check the file header in editImageItem and fail with IMAGE_EDIT_ERROR so the media editor falls back to /edit, and skip the download altogether when the attachment record already shows the original is too large. The record's dimensions describe the scaled copy once the big image threshold applies, so that pre-check only trusts a record without original_image. Suggested by ramonjd in review. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExwaM5APhmA5u75pBjyuLt
…s/gutenberg into try/csm-client-side-image-edits
…image-edits # Conflicts: # packages/upload-media/CHANGELOG.md
The trunk merge brought Prettier 3.9.6 and the new Markdown list style; reformat the one JS callsite and regenerate the two package READMEs so the branch matches what a clean CI install produces. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RtZdDWdC33cNHfY4U238A5
Type the editImageItem tests merged into the TypeScript private actions tests, and use a larger image for the too-large checks now that trunk raised the client memory budgets (#84980).
Trunk moved the cropper to @wordpress/image-editor and replaced the Reset button and aspect ratio select with a History menu and an aspect ratio menu, so wait on the new loaded class and drive the new controls.
|
I haven't forgotten this one, sorry! I've been working on getting the core image editor API into its own package. You'll have been seeing the conflicts 😄 Once it's done we can see how things might fit there. Cheers! |
Excellent - thanks for the update @ramonjd! |
Client-side media processing (WordPress/gutenberg#82362) edits images in the browser and uploads the result with a parent_image. The browser keeps the source's XMP but drops its C2PA manifest, so editing an image marked only in C2PA, such as a GPT image, would lose the marker. A wp_edited_image_metadata hook now writes the source's type into the edited file when it has none, and logs the changed file again on Simple so file storage gets it. Jetpack_WPCOM_AI_Marker caches each source's type for the request, so on core's server edit path the hook reuses the editor's read instead of downloading the original again.
| } | ||
| const post = | ||
| pendingEdits && 'post' in pendingEdits ? pendingEdits.post : media.post; | ||
| if ( post !== undefined ) { |
There was a problem hiding this comment.
Noticed while testing that editing an unattached image returns a 400 (post is not of type integer), because media.post is null.





Claude Code wrote this description from the branch, forwarded along:
AI Use
Code and description both written with 🤖 Claude Code, working from the plan in #82358. I will review and test.
Summary by CodeRabbit