Skip to content

Media: Preserve original_image metadata for large photos with EXIF rotation - #83785

Open
CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/preserve-original-image-on-scaled-rotation
Open

CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/preserve-original-image-on-scaled-rotation

Conversation

@CookieDarb

Copy link
Copy Markdown
Contributor

What?

Closes #83757

Fixes the bug where original_image in attachment metadata points to an intermediate rotated file (...-rotated-1.jpg) instead of the untouched uploaded file for large photos with EXIF rotation uploaded via client-side media processing.

Why?

In WordPress, large images exceeding the big image threshold (2560px) are scaled down and auto-rotated in a single step, preserving the untouched uploaded file as original_image.
In Gutenberg's client-side processing, large images with EXIF rotation were incorrectly treated as needing both a separate rotated file and a scaled file. Sideloading both files caused the intermediate rotated file to overwrite original_image in the attachment metadata, cutting off access to the true uploaded original file.

How?

  • packages/upload-media/src/store/private-actions.ts:
    • Check bigImageSizeThreshold using createImageBitmap before the rotation step to determine needsScaling.
    • Update needsRotation to exifOrientation !== 1 && ! needsScaling, ensuring that oversized images skip the separate rotated file and let the scaling step handle rotation, matching WordPress core.
    • Reuse needsScaling in the scaled sideload section to avoid redundant bitmap decoding.
  • packages/upload-media/src/store/test/private-actions.js:
    • Update makeHarness to accept store settings overrides.
    • Update the unit test skips the rotated-original sideload for already-scaled images to mock dimensions exceeding bigImageSizeThreshold: 2560 instead of relying on a mock filename.

Testing Instructions

  1. Ensure client-side media processing is active (window.crossOriginIsolated is true in browser console, or test on localhost / HTTPS).
  2. Have a JPEG image that has an EXIF orientation tag other than 1 and dimensions exceeding 2560px (e.g. truepic-20230212-camera.jpg [Orientation 6] mentioned in the issue).
  3. Open the Block Editor (post-new.php).
  4. Add an Image block and upload the oversized EXIF-rotated image.
  5. Wait for the client-side upload and thumbnail generation to finish.
  6. Check the attachment's original file path using WP-CLI or PHP:
   wp eval 'echo wp_get_original_image_path( <ATTACHMENT_ID> ) . PHP_EOL;'

Before Fix - returns rotated image: -rotated-1.jpg
After Fix - returns the untouched raw upload.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Investigating the root cause and aligning the fix with existing codebase patterns and unit tests. Final decisions and edits were made by me.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 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: CookieDarb <sakshamsharma5@git.wordpress.org>
Co-authored-by: Maurice1969DB <mauricevanloon@git.wordpress.org>
Co-authored-by: b1ink0 <b1ink0@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.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@Maurice1969DB

Copy link
Copy Markdown

Tested this against my reproduction from #83757 (WP 7.1.2, Chrome 153, wp-env, only Gutenberg active), using the plugin zip from CI.

With the PR, the Truepic photo (4032×3024, orientation 6) goes through one round of sideloads and gets finalized. original_image is the uploaded file again, and wp_get_original_image_path() returns it unchanged (same hash as the source). The scaled copy and the image sizes come out upright, and nothing is left in _wp_sideloaded_file. I did two runs, both the same.

The two controls (a large photo without rotation, a small rotated one) behave as before.

One thing I noticed: on 24.0.0 I didn't get the single round from the issue this time, but the older behaviour where everything is sideloaded twice and never finalized. With this PR that didn't happen either.

Looks good to me, thanks.

@b1ink0

b1ink0 commented Nov 20, 2026

Copy link
Copy Markdown

#87135 builds on this PR. It uses the needsScaling check that this PR moves to the top of generateThumbnails(), so I'll rebase it once this is merged.

One thing to watch when rebasing: #83530 wrapped the createImageBitmap() call in the scaled block in a try/catch, so an image the browser can't decode no longer gets stuck in thumbnail generation. This branch rewrites that block from before #83530, so the moved check needs to keep the try/catch.

@CookieDarb
CookieDarb force-pushed the fix/preserve-original-image-on-scaled-rotation branch from 46f4194 to 677a266 Compare November 23, 2026 06:44
@CookieDarb

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up! Rebased onto latest trunk and preserved the try/catch from #83530 around the moved createImageBitmap() check.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-side media: original_image names the rotated copy, not the upload, for a large photo with EXIF rotation

4 participants