Media REST API: Fix sideload and finalize for EXIF rotated images#12550
Media REST API: Fix sideload and finalize for EXIF rotated images#12550adamsilverstein wants to merge 2 commits into
Conversation
Treat an image_size=original sideload like a scaled sideload: the supplied file (the client-rotated original) replaces the attachment's main file and the untouched upload is kept as original_image. This is the same swap _wp_image_meta_replace_original() performs when the server rotates an image on upload. Accept transposed dimensions when validating an original sideload, since quarter-turn EXIF orientations (5-8) swap width and height; previously these uploads failed with rest_upload_dimension_mismatch. Reset the stored EXIF orientation to 1 in finalize for both original and scaled sub-sizes, as wp_create_image_subsizes() does, so exif_orientation no longer reports the pre-rotation value after the rotation has been applied. Skip original/scaled finalize entries missing the file name so a malformed payload cannot blank out the main file metadata.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
andrewserong
left a comment
There was a problem hiding this comment.
Thanks for opening this! It's a faithful backport of WordPress/gutenberg#80295 and I manually tested that this works as expected with the rotated file saved as the main source_url / media_details.file, with the original file as original_image.
LGTM!
Backport of WordPress/gutenberg#80295 (part of WordPress/gutenberg#77582).
Problem
In the client-side media upload flow, the client uploads the original file unmodified, applies the EXIF rotation itself, and sideloads the rotated file with
image_size=original. For quarter-turn orientations (EXIF 5-8) the rotated file's width and height are swapped relative to the stored metadata, sovalidate_image_dimensions()rejected the sideload with a 400 (rest_upload_dimension_mismatch) and the upload failed.The
originalsideload also stored the rotated file asoriginal_imagewhile the un-rotated upload stayed the attachment's main file, which is backwards relative to what core does when it rotates on upload.Solution
image_size=originalsideload like ascaledsideload: the supplied (rotated) file replaces the attachment's main file, and the untouched upload is kept asoriginal_image. This mirrors the swap_wp_image_meta_replace_original()performs inwp_create_image_subsizes().originalsideload.originalandscaledsub-sizes, aswp_create_image_subsizes()does for its scale and rotate paths, soexif_orientationno longer reports the pre-rotation value once rotation has been applied.original/scaledfinalize entries missing the file name so a malformed payload cannot blank out the main file metadata.Includes a parity test asserting the client-side path produces the same attachment metadata (dimensions, orientation,
original_image) as a classic server-side upload of the same EXIF-rotated fixture.Trac ticket: https://core.trac.wordpress.org/ticket/65643
Commit message