Repository navigation
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the 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. |
| public function edit_media_item( $request ) { | ||
| add_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); | ||
|
|
||
| $response = parent::edit_media_item( $request ); | ||
|
|
||
| remove_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); | ||
|
|
||
| return $response; | ||
| } |
There was a problem hiding this comment.
So all this is so we don't have to copy over edit_media_item to the plugin, which we can if folks think it's better.
There was a problem hiding this comment.
Pull request overview
This PR addresses a long-standing mismatch where REST image edits (rotate/crop/flip) are computed against an upright browser preview but applied server-side to the raw, un-uprighted pixels when the original file still has an unapplied EXIF orientation (common with iPhone JPEGs). It does so by ensuring the image editor used by /wp/v2/media/{id}/edit uprights the image before applying edit modifiers.
Changes:
- Switch
Gutenberg_REST_Attachments_Controllerto extend the WordPress 7.1 compat controller that now overridesedit_media_item()to apply EXIF-uprighting behavior for edits. - Add a request-scoped
wp_image_editorsfilter in the 7.1 compat controller to swap core image editors for orienting subclasses during edit requests. - Introduce two plugin-only image editor subclasses (Imagick and GD) that call
maybe_exif_rotate()onload().
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/media/load.php | Ensures the 7.1 compat controller is loaded before the main attachments controller so inheritance works. |
| lib/media/class-gutenberg-rest-attachments-controller.php | Updates the main controller to extend the 7.1 compat controller (inherits EXIF-aware edit behavior). |
| lib/compat/wordpress-7.1/class-gutenberg-rest-attachments-controller-7-1.php | Overrides edit_media_item() and temporarily swaps image editor classes to upright EXIF-oriented originals before edits. |
| lib/compat/wordpress-7.1/class-gutenberg-exif-orienting-image-editor-imagick.php | Adds an Imagick subclass that runs maybe_exif_rotate() during load(). |
| lib/compat/wordpress-7.1/class-gutenberg-exif-orienting-image-editor-gd.php | Adds a GD subclass that runs maybe_exif_rotate() during load(). |
| add_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); | ||
|
|
||
| $response = parent::edit_media_item( $request ); | ||
|
|
||
| remove_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); |
| public function edit_media_item( $request ) { | ||
| add_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); | ||
|
|
||
| $response = parent::edit_media_item( $request ); | ||
|
|
||
| remove_filter( 'wp_image_editors', array( __CLASS__, '_use_exif_orienting_image_editors' ) ); | ||
|
|
||
| return $response; | ||
| } |
|
Flaky tests detected in e062395. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/29181111768
|
There was a problem hiding this comment.
Not sure if it's the changes in this PR but I'm getting a 400 bad request when I attempt to test this out:
For testing, I'm trying to upload the file at packages/vips/src/test/fixtures/exif-rotated-90cw.jpg. The error occurs during Upload from the Image block:
Does this happen for you?
I haven't been able to reproduce the issue with images uploaded via my phone, or exported from Photos on my Macbook, with a variety of rotations, so not sure what might be causing it 🤔
Something else I was wondering while looking over the code change — would this be better to propose in wordpress-develop and leave it out of Gutenberg, or do you think that it's better to track the fix in GB, too? The main reason I wondered this is due to the comment that in core this is a one-liner, so wondered if it'd be simpler to handle it just in core.
Totally understand if you reckon it's better to keep it here alongside all the client-side and other media work, though!
|
Thank you for testing!
Yes, I'm seeing it on cc @adamsilverstein just in case that triggers any memories! I've also tested on various other images that do not normally behave, e.g., from my phone and the rotation so maybe the image has bad EXIF data? I'm not sure!
This occurred to me, and my rationale was that the Gutenberg editor would get the benefit of the change so if there's ever a plugin/core mismatch. That said, this has presumably been an issue forever so the one line in core would be okay by me. I have to get a backport ready anyway, so we can decide. I'll get that ready |
The /wp/v2/media/{id}/edit endpoint edits the file from
wp_get_original_image_path(), whose EXIF orientation may still be
unapplied: client-side uploads deliberately preserve the tag, and
server-side uploads only bake the rotation into the scaled copy, never
the original. Clients build edit modifiers against the oriented preview,
so edits landed in the wrong frame - rotating an EXIF-oriented iPhone
photo appeared to do nothing.
Swap the image editors for subclasses that call maybe_exif_rotate() on
load, scoped to edit_media_item() via the wp_image_editors filter. In
core the equivalent fix is a single line in edit_media_item(); the
scaffolding exists only because a plugin cannot add it there.
The client-side media controller now extends the 7.1 compat controller
so both controller paths inherit the fix.
e062395 to
273eaba
Compare
It's happening on trunk too for me. Strange though that when I created this PR yesterday I could upload it 🤔 |
PR here: I'm also now thinking we ditch this PR in favour of Core backport. Fewer things to juggle. |
Very strange... I wonder if something was committed in wordpress-develop that affects things 🤔
Sounds good, let's continue the discussion over in the wordpress-develop PR. My hunch with some of these sorts of things is that if it's been a bug forever and isn't super blocking things, we can fix in core rather than add to what we've got to wrangle over here. Thanks again for fixing this one! |
Very true! Closing this in favour of the Core patch in WordPress/wordpress-develop#12492 |
We did backport the size validation from Gutenberg to core somewhat recently, one guess is the validation isn't working as expected in Gutenberg, but worked once we backported it causing the error for mismatched uploads. I guess the sideload route should accept the rotated size. |
Note
If this looks good I'll go ahead and create a Core backport patch
What
Fixes image edits (rotate, crop, flip) landing in the wrong frame for photos whose EXIF orientation tag was never applied to their pixels, most visibly iPhone JPEGs. Rotating such a photo in the media editor modal or the Image block cropper previously appeared to do nothing.
See conversation #77582 (comment)
Why
The
/wp/v2/media/{id}/editendpoint edits the file fromwp_get_original_image_path(). That original often still carries an unapplied EXIF orientation tag: client-side uploads preserve it deliberately, and server-side uploads only bake the rotation into the scaled copy, never the original.Browsers apply the tag when rendering, so clients build their edit modifiers against the upright preview while the server applies them to the raw, unrotated pixels.
For an orientation 6 photo, a 90° rotation request produced pixels identical to what the user was already seeing, so the edit looked like a no-op. Crops had the same frame mismatch.
How
Gutenberg_REST_Attachments_Controller_7_1::edit_media_item()now swaps the image editors for subclasses that callmaybe_exif_rotate()on load, scoped to the request via thewp_image_editorsfilter. Edits then run in the same upright frame the client previewed.Two new editor subclasses (
Gutenberg_EXIF_Orienting_Image_Editor_Imagickand_GD) inlib/compat/wordpress-7.1/. These are plugin-only scaffolding, documented as such: in core the entire fix is one line inedit_media_item(), after the editor is created ($image_editor->maybe_exif_rotate();).Manual testing
packages/vips/src/test/fixtures/exif-rotated-90cw.jpgworks too).Before
Kapture.2026-07-12.at.15.23.04.mp4
After
Kapture.2026-07-12.at.15.22.06.mp4