Problem/Motivation
Drupal has various bugs with uploaded images that include image orientation from image EXIF data. Many modern phones use this, including iphone.
- With the default GD2 image toolkit, images appear rotated / upside down in Drupal because the toolkit discards EXIF orientation. There is a detailed description of this case in #3211441: Images incorrectly oriented because GD toolkit discards EXIF orientation.
- With the imagemagick toolkit, any image effects are applied incorrectly, for example the width and height may be swapped. The orientation is correct provided the browser supports it. According to can-i-use in September 2024, 95.4% of browsers did have this support, with most of the exceptions being software from 2020 or earlier.
Solutions in contrib
Enable EXIF Orientation, which resets all images to the natural orientation when the are first uploaded. There are some bugs which I hopefully fixed in #3477919: Refactor the image rotation logic.
With Image Effects module, add the 'auto orientation' effect at the start of all image styles. However this won't help if the image field has an upload constraint including maximum dimensions (with GD2, the EXIF orientation is lost on upload; with imagemagick the maximum dimensions are applied with height/width swapped, which is of course undetectable if they are equal).
Workaround
It seems likely the most image enhancement software (I tested GIMP) resaves the image without orientation, so avoiding the bug. This means that commercial sites with expert photographers potentially don't see the bug.
Proposed resolution
Create a new image toolkit operation named AutoOrient.
Reset orientation on upload, similar to EXIF Orientation module. This works by creating a new upload constraint that runs AutoOrient, which we can add in ImageWidget::formElement().
- + Simple and the existing code is close to what we want in Core.
- - The extra conversion could reduce image quality slightly (however the phone image is likely much higher resolution than needed for web, so it's likely irrelevant).
Alternative
Reset orientation at the start of processing for an image style, similar to Image Effects. This works by creating a new 'auto orient' image effect. However we should run it automatically, without requiring the admin to configure it, and also hide it from the image style UI.
- - Doesn't work with a max resolution upload constraint (until we have #2831529: Instead of just resizing original image, select an image style to apply on the image being uploaded).
- - Images in the filesystem do still have an EXIF orientation that Drupal can't understand. For example if a module attempted to display image dimensions reading from the metadata, the values could be reversed.
- + This works with existing uploaded files that already contain an orientation (but of course not if it was stripped by an upload constraint).
Remaining tasks
User interface changes
API changes
Data model changes
Original report
so...i still see rotated image issues...
we fought this in d7, and hoped for better image orientation detection in d8...
Example:
http://foodtron.org/node/11
What does the wisdom of the group recommend?
Do we need another exif autodetect magic hack module?
this has been an issue for over 4 years...inherited by d8...
It was just a normal photo taken on my iphone 6s...and i remember this all the way back to iphone 3...
same pic on the phone via email comes out fine. attaching original pic to this thread.
@geerlingguy suggests:
The 'auto orientation' effect is part of 'Image Effects' (the spiritual successor to imagecache actions from D7): https://www.drupal.org/project/image_effects
See: https://github.com/drupal-media/image_effects#introduction (it's supported by both GD and ImageMagick).
Looks like it's not in core, though :(
Looks like this (https://www.drupal.org/node/2284577) is the closest thing to possibly making that happen. Outside of the media project, it looks like there's not much interest in doing the work :(
@barrett suggests:
And thus round 5274 of the Small Core vs Expected OOTB Functionality fight is begun....
It seems reasonable to me that whatever system provides image upload and display functionality should also have capability to figure out which way is up in the image.
@mark-trapp suggests:
This tends to happen when taking photos using a mobile device that allows you to access the shutter with a hardware button (like a volume button): iPhones and some (most?) Android phones have this feature.
Here's an article explaining the problem for iPhone (though you can find people confused about it on Android as well): http://iphonephotographyschool.com/iphone-photos-upside-down/
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 2664632-17.patch | 10.08 KB | morenstrat |
| image1.jpeg | 3.37 MB | jacov |
Comments
Comment #2
swentel commentedIt's available in https://www.drupal.org/project/image_effects so that's the way to go for now.
Moving to feature request.
Comment #3
wim leersComment #4
jacov commentedComment #5
jacov commentedComment #6
tim.plunkettThat's not a real issue tag. And the issue summary is now missing the "Remaining tasks", "User interface changes", "API changes", and "Data model changes" sections...
Comment #7
jacov commentedComment #8
mondrakeIMHO the problem here is a bit more complicated.
Adding an 'Auto orientation' effect will only work on building derivative images, i.e. on image files that are already uploaded.
But:
1) should it be added to all default styles (thumbnail, medium, large, etc.)? If so, why not to any style? But in that case, logically the auto orientation is not an effect, but something that needs to happen earlier than the image style loop of effects.
2) the Image field allows to set maximum image dimensions on the field definition (actually they are called 'Maximum image resolution', but there's a #847098: Revise image field min/max settings help text form for clarity to address that). Now, if an uploaded image exceeds those dimensions, it will be scaled to the maximum dimensions in
file_validate_image_resolutionbefore saving to disk. The scaling is performed via the image toolkit operation, which in the case of GD toolkit leads to dropping any EXIF metadata from the uploaded image. Result: it will no longer be possible to auto orient the uploaded image during a derivative generation process.So it looks to me if this has to be addressed in core, it should rather be addressed on the Image field level, by adding an option to autorotate the image at the time of upload before the Exif info is lost. This will have the benefit of not needing to have an autorotation effect on all the image styles. But this would need (a) an autorotate image toolkit operation in core for GD, which in turn would require (b) to have core supporting a standard approach on getting Exif metadata from images, so that other toolkits can provide their own implementation. For (b), there's #2630242: Provide methods to retrieve EXIF image information via the Image object for discussion.
Comment #9
dman commentedGood analysis from @mondrake
We pondered much the same questions over in imagecache_actions when we introduced the autorotate action. I knew that a post-processing plugin wasn't *really* the appropriate place for such a process to live, yet could not imagine at what point further upstream it should live.
I agree - If this were to be added, it should be at a higher point in the system, like at upload/registration time, with a global flag that autocorrects all images if possible.
But EXIF support in PHP was significantly tedious. Hopefully there is a much nicer toolkit library to help us with that
Comment #12
mondrakeSee #2831529: Instead of just resizing original image, select an image style to apply on the image being uploaded for a proposal.
Comment #13
jacov commentedthis is still a big problem.
majority of pictures coming from smartphones today are iphone, hence this makes Drupal buggy out of the box.
see issue here:
http://slime.bar/
Comment #14
tim.plunkettIt can be major, but the absence of an important feature is not a bug, it's still a feature request.
Comment #16
morenstratComment #17
morenstratThis patch simply copies the auto orient GD operation and auto orient image effect from the Image Effects moduleand adds them to Core. Namespaces, class names and annotations were adapted, particularly the GD operation name in order to not collide with the module's operation.
Comment #18
hansfn commentedI have tested the patch in comment 17 and it works perfectly for Drupal 8.2.6. I know this comment (and status change) isn't enough to get the patch into core, but at least other people finding this issue can know that it actually works.
PS! After applying the patch, clear cache and add the "Automatically correct orientation" effect as the first effect for all (used) image styles.
Comment #19
wim leersIs "orientate" correct English?
Pointless comment.
+
+
+- 1 = Horizontal (normal)
- 3 = Rotate 180
- 6 = Rotate 90 CW
- 8 = Rotate 270 CW
+
+
+
+
+
Wikipedia: Exchangeable image file format
+ "),
This looks… strange.
This is not testing if EXIF is supported. This is hardcoding a list of MIME types. Let's then move them to a class constant.
Pointless comment.
Same test again. Should probably then also have a protected helper method.
Do we need to fix this TODO or not?
Also: s/conmments/comments/
Yes, this is the most important information: this is very much missing test coverage.
Comment #20
mondrakeRe. #19.7 and #19.8, see #2857260: Auto orientation: GD toolkit operation does not cover all cases, tests missing in the contrib module.
In general, just moving the effect/toolkit ops from contrib to core does not address comments between #8 and #12.
Comment #21
nathaniel commentedThis contrib module seems to be working nicely for iPhone photos:
https://www.drupal.org/project/exif_orientation
It rotates the images in hook_file_presave, so it happens before file_validate_image_resolution in the photos module.
Comment #23
alan d. commentedJust found that this is an incomplete solution for the EXIF orientation, there are some "Flipped" versions that need to be rotated and flipped... which is a bigger issue requiring image rotate to handle this, so a followup child issue rather than a reason to hold this one up.
I'm sure there are better references, but this was the first one I found when looking for what a 5 state meant (degrees = 90 AND flipped)
http://www.impulseadventure.com/photo/exif-orientation.html
Comment #24
mondrake@Alan D. these cases were dealt with in Image Effects #2857260: Auto orientation: GD toolkit operation does not cover all cases, tests missing, see #20. If the current version of the effect there works as expected, then it might be worth updating the patch here to reflect those changes.
Comment #36
candelas commentedHello,
Any patch for Drupal 11 and 10?
Thanks
Edit: I see that Image Effects has the option, but it would be nice to have it in core for people that don't use this module.
Comment #37
adamps commentedI've just hit the rotated images problem on a site, so I've done some research.
This issue seems to be unclear:
As far as I can see, in 2024 most browsers do support EXIF orientation. The main reason for the wrong orientation is that Drupal has stripped the EXIF metadata without correcting the orientation. The GD library appears to do this when it applies most image effects.
So analysing the proposed solution:
FileImageDimensions). In this case the correct orientation is lost before the file is ever saved, so there's no possible fix.So in summary I believe that the "Auto orientation image effect" would be either a poor fix or no fix at all to the problem/motivation.
Comment #38
adamps commentedThere is a similar issue #3211441: Images incorrectly oriented because GD toolkit discards EXIF orientation that is less active, but seems much more clear. The title and problem/motivation are consistent, and the proposed resolution is much more heading in the right direction: add exif_orientation module code into core.
I took a quick look and here's a quick summary of what I found:
Still, the module is a good starting point, and it helped point me in the right direction, so thank you to the developers and maintainers.
Comment #39
adamps commentedI propose instead that we make the fix in the place where the problem is introduced: GDToolkit. The GD2 library loses EXIF data on many operations. Before doing such an operation, then the Drupal wrapper code should apply auto-orientation.
Perhaps another image toolkit is able to do many operations preserving EXIF data, in which case it won't need the same fix. A site admin may have chosen the other image toolkit exactly because they needed to preserve EXIF copyright statements. Therefore the fix should be specific to the image toolkit wrapper for whatever operations need it in that toolkit.
Comment #40
adamps commentedComment #41
adamps commentedSorry I belatedly see that I have duplicated #8 however that information wasn't in the IS. Hopefully I've helped future readers understand the key point by @mondrake.
Comment #42
adamps commentedI updated the IS with all my findings. I feel it's a bug not a feature, however feel free to reset if you don't agree.