Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[image_picker] Fix images changing to incorrect orientation #7187

Merged
merged 4 commits into from
Feb 17, 2023

Conversation

vashworth
Copy link
Contributor

@vashworth vashworth commented Feb 16, 2023

When you take an image with an iPhone in portrait mode, it's actually taking the image in landscape mode and saving rotation information into the image's metadata. The UIImage class will then automatically transform the image to be the correct orientation. You can read more about it here.

Now, in the plugin code, when the user resizes an image that has orientation metadata, we resize it differently to handle the rotation.

// Scaling the image always rotate itself based on the current imageOrientation of the original
// Image. Set to orientationUp for the orignal image before scaling, so the scaled image doesn't
// mess up with the pixels.
UIImage *imageToScale = [UIImage imageWithCGImage:image.CGImage
scale:1
orientation:UIImageOrientationUp];
// The image orientation is manually set to UIImageOrientationUp which swapped the aspect ratio in
// some scenarios. For example, when the original image has orientation left, the horizontal
// pixels should be scaled to `width` and the vertical pixels should be scaled to `height`. After
// setting the orientation to up, we end up scaling the horizontal pixels to `height` and vertical
// to `width`. Below swap will solve this issue.
if ([image imageOrientation] == UIImageOrientationLeft ||
[image imageOrientation] == UIImageOrientationRight ||
[image imageOrientation] == UIImageOrientationLeftMirrored ||
[image imageOrientation] == UIImageOrientationRightMirrored) {
double temp = width;
width = height;
height = temp;
}

#7084 changed the plugin so that every image returns basic metadata, which includes the orientation/rotation info. However, it wasn't doing to metadata scaling process which was causing the images to get incorrect orientation.

Fixes flutter/flutter#120356.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@vashworth
Copy link
Contributor Author

vashworth commented Feb 16, 2023

@cyanglaz @jmagman Any ideas on how to test this? I could use a real image to test it, but I'm struggling to get a small image with the orientation metadata in it. For example, I can take a picture on my iPhone and it will have the orientation data in it and is 260KB. If I resize it or compress it, it loses the orientation metadata.

@jmagman
Copy link
Member

jmagman commented Feb 16, 2023

@cyanglaz @jmagman Any ideas on how to test this? I could use a real image to test it, but I'm struggling to get a small image with the orientation metadata in it. For example, I can take a picture on my iPhone and it will have the orientation data in it and is 260KB. If I resize it or compress it, it loses the orientation metadata.

Can you re-add the orientation metadata with something like exiftool? There also might be some imagemagick flags that resize/compress without stripping the metadata.

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jmagman
Copy link
Member

jmagman commented Feb 16, 2023

LGTM

Once the formatter is happy.

@vashworth vashworth added autosubmit Merge PR when tree becomes green via auto submit App and removed autosubmit Merge PR when tree becomes green via auto submit App labels Feb 16, 2023
@auto-submit auto-submit bot merged commit 9a3a77e into flutter:main Feb 17, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Feb 17, 2023
* cd09d9d31 [ci] Update iOS simulator (flutter/plugins#7131)

* 016c3b7f1 Roll Flutter from df41e58 to 22e17bb (28 revisions) (flutter/plugins#7186)

* 7160f55e8 [ios_platform_images] Update minimum version to iOS 11 (flutter/plugins#6874)

* ea048a249 [in_app_purchase] Update minimum Flutter version to 3.3 and iOS 11 (flutter/plugins#6873)

* 530442456 [google_sign_in_web] Migrate to the GIS SDK. (flutter/plugins#6921)

* 9a3a77e6c [image_picker] Fix images changing to incorrect orientation (flutter/plugins#7187)

* 8f3419be5 Roll Flutter from 22e17bb to 298d8c7 (20 revisions) (flutter/plugins#7190)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App needs tests p: image_picker platform-ios
Projects
None yet
2 participants