Skip to content

REST API: Apply unapplied EXIF orientation before image edits - #80144

Closed
ramonjd wants to merge 2 commits into
trunkfrom
try/image-editor-compensate-exif-rotation
Closed

ramonjd wants to merge 2 commits into
trunkfrom
try/image-editor-compensate-exif-rotation

Conversation

@ramonjd

@ramonjd ramonjd commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

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}/edit endpoint edits the file from wp_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 call maybe_exif_rotate() on load, scoped to the request via the wp_image_editors filter. Edits then run in the same upright frame the client previewed.

Two new editor subclasses (Gutenberg_EXIF_Orienting_Image_Editor_Imagick and _GD) in lib/compat/wordpress-7.1/. These are plugin-only scaffolding, documented as such: in core the entire fix is one line in edit_media_item(), after the editor is created ($image_editor->maybe_exif_rotate();).

Manual testing

  1. Upload a JPEG with an EXIF orientation tag, for example a portrait photo taken on an iPhone (packages/vips/src/test/fixtures/exif-rotated-90cw.jpg works too).
  2. Open it in the media editor modal, rotate it 90°, and save.
  3. Confirm the saved image is actually rotated. Before this change it looked identical to the original.
  4. Repeat with a crop: crop a distinctive corner and confirm the saved crop matches the region you framed.
  5. Edit the saved (already upright) copy again and rotate once more. Confirm it rotates exactly once, with no double rotation.
  6. Check for regressions on regular images

Before

Kapture.2026-07-12.at.15.23.04.mp4

After

Kapture.2026-07-12.at.15.22.06.mp4

@ramonjd
ramonjd requested a review from spacedmonkey as a code owner July 12, 2026 05:24
@ramonjd ramonjd self-assigned this Jul 12, 2026
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Feature] Media Anything that impacts the experience of managing media labels Jul 12, 2026
@github-actions

github-actions Bot commented Jul 12, 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. You can also manually ask me to refresh this list by adding the props-bot label.

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: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines +122 to +130
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;
}

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.

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.

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.

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_Controller to extend the WordPress 7.1 compat controller that now overrides edit_media_item() to apply EXIF-uprighting behavior for edits.
  • Add a request-scoped wp_image_editors filter 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() on load().

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

Comment on lines +123 to +127
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' ) );
Comment on lines +122 to +130
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;
}
@github-actions

Copy link
Copy Markdown

Flaky tests detected in e062395.
Some 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.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/29181111768
📝 Reported issues:

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

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:

Image

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:

Image

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!

@ramonjd

ramonjd commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

Thank you for testing!

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:

Yes, I'm seeing it on /wp-json/wp/v2/media/<id>/sideload, triggered by validate_image_dimensions

{
    "code": "rest_upload_dimension_mismatch",
    "message": "Uploaded image dimensions (32x64) do not match original image dimensions (64x32).",
    "data": {
        "status": 400
    }
}

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!

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?

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.
@ramonjd
ramonjd force-pushed the try/image-editor-compensate-exif-rotation branch from e062395 to 273eaba Compare July 13, 2026 07:25
@ramonjd

ramonjd commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

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:

It's happening on trunk too for me. Strange though that when I created this PR yesterday I could upload it 🤔

@ramonjd

ramonjd commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

I have to get a backport ready anyway, so we can decide.

PR here:

I'm also now thinking we ditch this PR in favour of Core backport. Fewer things to juggle.

@andrewserong

Copy link
Copy Markdown
Contributor

It's happening on trunk too for me. Strange though that when I created this PR yesterday I could upload it 🤔

Very strange... I wonder if something was committed in wordpress-develop that affects things 🤔

I'm also now thinking we ditch this PR in favour of Core backport. Fewer things to juggle.

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!

@ramonjd

ramonjd commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

My hunch with some of these sorts of things is that if it's been a bug forever and isn't super blocking things

Very true!

Closing this in favour of the Core patch in WordPress/wordpress-develop#12492

@ramonjd ramonjd closed this Jul 13, 2026
@adamsilverstein

adamsilverstein commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

Very strange... I wonder if something was committed in wordpress-develop that affects things 🤔

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.

@t-hamano
t-hamano deleted the try/image-editor-compensate-exif-rotation branch July 21, 2026 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Media Anything that impacts the experience of managing media [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants