Skip to content

Media: Where should EXIF rotation belong — server, client, or hybrid? #77582

Closed

Description

@adamsilverstein

Background

In the discussion on #75793 (comment), the question came up of whether EXIF rotation detection is best done client-side rather than relying on the server. We agreed to continue the discussion in a follow-up — this is that follow-up.

Current state

EXIF orientation is currently detected server-side and rotation is applied client-side:

  • Server (PHP): wp_read_image_metadata() reads the EXIF orientation during upload and stores it in attachment metadata. Gutenberg_REST_Attachments_Controller::prepare_item_for_response() exposes it as exif_orientation on the REST response (lib/media/class-gutenberg-rest-attachments-controller.php:262).
  • Server (PHP): wp_image_maybe_exif_rotate is filtered to __return_false during the client-side upload flow so the server does not rotate the original (lib/media/class-gutenberg-rest-attachments-controller.php:382).
  • Client (JS): generateThumbnails() reads attachment.exif_orientation from the response and dispatches a rotation operation when it isn't 1 (packages/upload-media/src/store/private-actions.ts:1096).

So today, detection lives in PHP (wp_read_image_metadata) and rotation lives in JS (vips). The orientation value round-trips through the upload response.

Three approaches

1. Server-side detection (current)

The browser uploads the original; the server reads EXIF and returns the orientation in the response; the client rotates and sideloads sub-sizes.

Pros

  • Single source of truth — reuses wp_read_image_metadata(), which all of WordPress already trusts.
  • Backwards compatible with any existing PHP filters/plugins that hook into the metadata reader.
  • Works for any MIME type the server can parse (HEIC, TIFF, etc.) — no need to ship a JS EXIF parser per format.
  • Consistent results across browsers (no JS EXIF library quirks).

Cons

  • Detection is delayed until after the original upload completes — rotation can't begin until the response arrives.
  • Adds an unavoidable PHP step even when the client already has the file in memory.
  • Couples the client-side processing pipeline to a server round-trip for a piece of information the client could compute locally.

2. Client-side detection

Read EXIF orientation in JS (e.g. via exifr, a small custom parser, or createImageBitmap with imageOrientation: 'from-image') before/during upload and rotate locally without waiting for the server.

Pros

  • No server round-trip needed before rotation — pipeline can run end-to-end in the browser.
  • Lets us rotate the original before upload, so the server stores an already-correct file (matches iOS/Android camera behavior).
  • Simpler data flow — no exif_orientation field on the REST response.

Cons

  • Need to parse EXIF in JS for every supported format. HEIC/HEIF EXIF parsing in particular is non-trivial and varies by browser.
  • Bypasses any PHP filter that plugins use to override orientation handling (loss of wp_image_maybe_exif_rotate-style extensibility).
  • Potential cross-browser inconsistency. createImageBitmap({ imageOrientation: 'from-image' }) is well-supported but doesn't give us the raw orientation value, only the rotated bitmap.
  • Not all browsers support client side media so the server would still be a fallback meaning we need to maintain both paths

3. Hybrid

Detect client-side when possible (fast path), fall back to server-side detection (already in the response) when the client can't read EXIF for that format. Or: detect client-side and verify server-side.

Pros

  • Best of both: fast path for common cases, safety net for edge cases.
  • Lets us migrate incrementally — keep the server path, add the client path opportunistically.

Cons

  • Two implementations to keep in sync.
  • More complex contract: when does the client trust its own reading vs. the server's? Conflict resolution rules need to be specified.
  • The "best of both" can become "worst of both" if the fast path is rarely a win in practice.

Questions for the discussion

  1. . How important is the ability for plugins to filter orientation handling in PHP? How many popular plugins use filters that would be lost?
  2. Are there formats (HEIC, TIFF) where client-side EXIF parsing is too costly to ship? (we already do this for HEIC orientation when the server lacks support)

Related: #75793, the broader client-side media processing initiative ([Feature] Client Side Media).

Activity

  1. added
    [Type] Discussion
    [Feature] Client Side Media
    on Apr 23, 2026
  2. adamsilverstein commented on Jun 19, 2026

    @adamsilverstein
    MemberAuthor

    My take on this is the checks should remain on the server for any formats the server can handle. when the server can't handle the mime type (for example HEIC), we should do our best to detect the rotation on the client (which is the current approach for HEIC). The advantages I see are:

    1. The existing code continues to be used - no regressions
    2. The existing PHP filter (wp_read_image_metadata https://developer.wordpress.org/reference/hooks/wp_read_image_metadata/) continues to run as expected
    3. The client can still handle formats the server can't (loosing advantages 1 & 2 though)

    Any additional feedback on this or things we should consider @andrewserong or @swissspidy?

  3. andrewserong commented on Jun 24, 2026

    @andrewserong
    Contributor

    My take on this is the checks should remain on the server for any formats the server can handle. when the server can't handle the mime type (for example HEIC), we should do our best to detect the rotation on the client (which is the current approach for HEIC).

    This sounds like a good plan to me. The only thought I had when looking over #79384 was just that I don't personally have any background on parsing these sorts of file formats. If we're doing this part client-side, we'll just want to make sure we're documenting file specs etc so that we can verify correctness — I mostly mention this because I'm finding with some of these more complex PRs it can be tricky to review beyond a "this seems to work for me" happy path, and I often don't quite know how to tell if there are edge cases we're missing, things like that.

    To put it more simply: this is a good idea, but let's consider how to mitigate the tech debt by ensuring we can easily find the path to either a) switch it off if it proves problematic or b) how to fix things / update if there edge cases we haven't thought of.

    What do you think?

  4. swissspidy commented on Jun 24, 2026

    @swissspidy
    Member

    Detection is delayed until after the original upload completes — rotation can't begin until the response arrives.

    How's the user experience? Do I drop an image into the editor and after a few seconds it is suddenly rotated, and with the client-side approach it would be near instant? Or can't you really tell the difference?

  5. adamsilverstein commented on Jun 24, 2026

    @adamsilverstein
    MemberAuthor

    Detection is delayed until after the original upload completes — rotation can't begin until the response arrives.

    How's the user experience? Do I drop an image into the editor and after a few seconds it is suddenly rotated, and with the client-side approach it would be near instant? Or can't you really tell the difference?

    That is an excellent question and I will investigate. I would imagine doing everything client side would be a better user experience as you hinted, a large upload or slow network could lead to a significant delay before the rotation is determined if we wait for the server round trip.

    This sounds like a good plan to me. The only thought I had when looking over #79384 was just that I don't personally have any background on parsing these sorts of file formats.

    We have a well tested solution in core for existing supported formats; for newer formats where we use the client approach, we can follow that pattern and also add tests that validate at least an initial set of test images work as expected (with various ingoing rotations). There may very well be edge cases we need to address in the future, but we won't find them until we release this to users. As long as we can address the most likely cases (ie. user takes portrait photo with iPhone and uploads from computer), we should be fine for now.

  6. adamsilverstein commented on Jun 24, 2026

    @adamsilverstein
    MemberAuthor

    Detection is delayed until after the original upload completes — rotation can't begin until the response arrives.

    How's the user experience? Do I drop an image into the editor and after a few seconds it is suddenly rotated, and with the client-side approach it would be near instant? Or can't you really tell the difference?

    @swissspidy Turns out you really can't tell the difference, the browser typically auto-rotates the original image correctly for display already.

    From Claude: When you drop an image, the editor immediately shows a blob URL of the raw original as the preview. Browsers apply image-orientation: from-image by default, so that preview is already correctly oriented, instantly, with no EXIF parsing on our side. The rotate step isn't fixing what's on screen, it's baking the rotation into the pixels and stripping the orientation flag so the stored sub-sizes are correct for downstream consumers that don't honor EXIF (themes, feeds, emails). When it finishes it swaps in a new blob URL that looks identical. So in both approaches the user sees a correctly-oriented image from the first frame, with no visible "snap." The server round-trip delays when the corrected bytes are produced, not when the user perceives rotation.

    I can double check this, but it sounds right to me.

    There are still some potential wins of client-side detection are architectural:

    • Rotate the original before upload, so the server stores an already-correct file.
    • Simpler data flow, no exif_orientation round-tripping through the REST response.

    But we also loose some advantages of the server round trip, namely:

    • honoring existing filters and
    • a lower chance of getting something wrong (same code so should match existing behavior)

    I did a search for existing plugin usage and did see several plugins using the existing filters (>20k sites total), it would be nice of us to not break these plugins:

    Image
  7. ramonjd commented on Jun 24, 2026

    @ramonjd
    Member

    I ran into a concrete case while testing out image cropping, namely uploading HEIC files on mobile Safari. They tend to get falsely rotated after cropping.

    I'm pretty sure it's something to do with double-rotation. From what I've gathered the browser auto-applies EXIF orientation when decoding for canvas, then the server returns exif_orientation and the client rotates again on top.

    For this case, would createImageBitmap({ imageOrientation: 'from-image' }) before drawing to the crop canvas fix the display side? And then stripping EXIF orientation tag before upload stop the server from triggering a second rotation?

    If so, it seems like a good argument for the hybrid approach on the surface. So the client handles it when it can, server is a fallback.

    I'm not sure what this means for performance. Some of the HEIC files on my phone are 10MB+ I can only guess that stripping a tag is much cheaper than rotating a 10MB++ image using vips rotation (??)

  8. adamsilverstein commented on Jun 26, 2026

    @adamsilverstein
    MemberAuthor

    I ran into a concrete case while testing out image cropping, namely uploading HEIC files on mobile Safari. They tend to get falsely rotated after cropping.

    Thanks for the feedback @ramonjd - did you upload them from your phone? using the web wp-admin, not the ios app, right? I thought iOs auto-converted these to jpegs on upload. did you wind up with a portrait image displayed sideways?

  9. ramonjd commented on Jun 29, 2026

    @ramonjd
    Member

    did you upload them from your phone? using the web wp-admin

    Yeah, just via the block editor.

    did you wind up with a portrait image displayed sideways?

    This, and also not respecting the rotation on the backend (via the edit route in the attachments controller). I tested a few times and pretty sure it wasn't user error 😅 It's only safari that's doing this:

    ScreenRecording_06-30-2026.09-53-12_1.mp4

    Just in case, I tested around with createImageBitmap({ imageOrientation: 'from-image' }) but that didn't fix my particular issue and I think it's a well documented quirk, hence why I assumed EXIF rotation at upload might help.

  10. adamsilverstein commented on Jul 6, 2026

    @adamsilverstein
    MemberAuthor

    @ramonjd - I'm curious if this is specific to Safari mobile. Could you test the same flow with Safari desktop and also if possible test with Chrome mobile? The HEIC handling logic falls thru several possible approaches depending on the browser (and platform) capabilities, so I'm interested to gather more data to enable writing a test and fix for the rotation issue.

  11. ramonjd commented on Jul 7, 2026

    @ramonjd
    Member

    Could you test the same flow with Safari desktop and also if possible test with Chrome mobile?
    Thanks for following up.

    I see it on Safari and Chrome mobile. Same flow. I tried Brave mobile as well. Same thing. I used the same HEIC image.

    So I think my testing was muddled, before. Sorry. Does that match your expectations?

  12. adamsilverstein commented on Jul 12, 2026

    @adamsilverstein
    MemberAuthor

    Could you test the same flow with Safari desktop and also if possible test with Chrome mobile?
    Thanks for following up.

    I see it on Safari and Chrome mobile. Same flow. I tried Brave mobile as well. Same thing. I used the same HEIC image.

    So I think my testing was muddled, before. Sorry. Does that match your expectations?

    @ramonjd We just merged a potential fix in #79384, can you see if the issue is resolved for you?

  13. ramonjd commented on Jul 12, 2026

    @ramonjd
    Member

    @ramonjd We just merged a potential fix in #79384, can you see if the issue is resolved for you?

    Tested again in Chrome/Safari mobile (playground + gutenberg trunk) and Safari desktop and all exhibit the same thing when uploading images taken by an iphone camera:

    Kapture.2026-07-12.at.12.56.46.mp4
    1. The uploading snackbar persists (goes away when you save the post)
    2. image rotation doesn't take effect

    I'll look into if it's something specific to the image cropping tool.

    Thanks for following up on that!

  14. ramonjd commented on Jul 12, 2026

    @ramonjd
    Member

    We just merged a potential fix in #79384, can you see if the issue is resolved for you?
    I'll look into if it's something specific to the image cropping tool.

    So, I needed to school myself a bit here, and I don't think there's an overlap. So that PR baked EXIF into sub sizes right? We're working with the the full-size original in the editor after upload I believe.

    My current assumption is that the image editor needs a tweak so that the rotation is compensated for in the browser. The cropper works with raw landscape pixels. I mistakenly assumed any HEIC or other rotated image would be somehow "normalized"

    I'll see if there's a fix. Thanks again!

  15. ramonjd commented on Jul 12, 2026

    @ramonjd
    Member

    I'll see if there's a fix. Thanks again!

    Currently I can get a working on the server, in the attachment controller when we do the image manipulation, just before save

    $image_editor = wp_get_image_editor( $image_file_to_edit );
    $image_editor->maybe_exif_rotate(); 

    See maybe_exif_rotate(), which also reads metadata and applies wp_image_maybe_exif_rotate

    PR: #80144

    Not sure how it directly relates to this conversation - I guess if there were a way to do it on the clientside, we'd try that first (?)

  16. adamsilverstein commented on Jul 12, 2026

    @adamsilverstein
    MemberAuthor

    I'll look into if it's something specific to the image cropping tool.

    I didn't understand until this post that you were using the rotation feature, I'll review your PR and test that as well, I haven't been testing there. I'll also re-test uploading a rotated image manually to make sure its working as expected.

  17. andrewserong commented on Jul 14, 2026

    @andrewserong
    Contributor

    @adamsilverstein, while testing #80144, @ramonjd and I noticed that the file packages/vips/src/test/fixtures/exif-rotated-90cw.jpg fails to upload the sideload request in trunk with a 400 response (#80144 (review)):

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

    I.e the request for this one was to the sideload endpoint: http://localhost:8888/wp-json/wp/v2/media/1214/sideload?_locale=user

    Are you able to reproduce? It sounds like it's related to EXIF rotation in some way, and we couldn't reproduce with rotated images from our phones, but this file in particular seems to be a way to test it. I've run out of time today to look into this further, but I'm wondering if there's some nuance to the rotation logic we might have missed somewhere that's causing the failed request? (Also wasn't sure if the issue is Gutenberg-side or wordpress-develop-side).

  18. andrewserong commented on Jul 15, 2026

    @andrewserong
    Contributor

    but I'm wondering if there's some nuance to the rotation logic we might have missed somewhere that's causing the failed request?

    I've put up a tentative fix in #80295, but would love some feedback on whether or not that's the right direction. Happy to close it out if it's no good, or open a wordpress-develop / core fix / backport if it looks okay.

  19. adamsilverstein commented on Jul 15, 2026

    @adamsilverstein
    MemberAuthor

    @adamsilverstein, while testing #80144, @ramonjd and I noticed that the file packages/vips/src/test/fixtures/exif-rotated-90cw.jpg fails to upload the sideload request in trunk with a 400 response (#80144 (review)):

    Oh, good catch, that does look like a bug!

  20. adamsilverstein commented on Jul 17, 2026

    @adamsilverstein
    MemberAuthor

    Closing this one as resolved, thanks for the discussion and work here.

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

Metadata

Metadata

Labels

[Feature] Client Side Media[Type] Discussion

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions