2

I'm having issues with imagecreatefromjpeg and image rotation.

To be more specific:

Image #1 https://dl.dropboxusercontent.com/u/9271284/stackoverflow/20160124112643911541_orig.jpg is rotated properly by imagecreatefromjpeg (exif tag is honored) but not in Picasa or Win10 Photo viewer.

There is no rotation in my code, when I say "is rotated properly by imagecreatefromjpeg" I mean it, literary. Simply by doing

$img = imagecreatefromjpeg($filename);

image is loaded from file and rotated properly. I'm running PHP 5.5.31-2+deb.sury.org~trusty+1 with GD version 2.1.1-dev.

Image #2 https://dl.dropboxusercontent.com/u/9271284/stackoverflow/20160125070736520868_orig.jpg is not rotated properly by imagecreatefromjpeg (exif tag is not honored) but is rotated properly in Picasa and Win10 Photo viewer.

Also, using excellent Jeffrey's Exif Viewer http://regex.info/exif.cgi results are consistent with what I'm seeing from imagecreatefromjpeg.

Vnuk
  • 2,673
  • 1
  • 32
  • 49
  • Could you show some rotation code? – Alex Blex Jan 25 '16 at 09:40
  • I should've been more clear in my question, there is no rotation code. I'll update. – Vnuk Jan 25 '16 at 09:45
  • So, how do you know if it is rotated or not? `getimagesize` returns correct values for both images. – Alex Blex Jan 25 '16 at 10:26
  • I have eyes so I can see that it's not rotated. As I've said, I'm getting the exact same behavior from imagecreatefromjpeg and Jeffrey's Exif Viewer. – Vnuk Jan 25 '16 at 10:30
  • Sorry, may be my eyes lie to me, but exif tags for the second image read `'ExifImageWidth' =>3264, 'ExifImageLength' =>2448`, i.e. landscape layout. It is exactly what I see in Jeffrey's Exif Viewer page. – Alex Blex Jan 25 '16 at 10:37
  • Yes, and that is fine. However, when that image is loaded into imagecreatefromjpeg it is not rotated and I should do imagerotate($img, -90) for it. However, that is not necessary for first image, and that is my question all along. – Vnuk Jan 25 '16 at 10:41
  • Clearly I don't see what's the problem. Both images are displayed with landscape layout according to exif tags. If you rotate it it is portrait layout. – Alex Blex Jan 25 '16 at 10:57
  • First image is NOT displayed in landscape using Jeffrey's Exif Viewer and imagecreatefromjpeg. I do not have a way to detect if I need to rotate the image using imagerotate or not. – Vnuk Jan 25 '16 at 11:08
  • I just found this question via google. I had a Nokia Lumia phone in the past. I opened photos it took using `imagecreatefromjpeg` and they were shown in correct orientation. Today, I opened pictures taken with a S9 phone and orientation is not correct. Trying to understand the reason. – Bernhard Döbler Aug 13 '18 at 21:06

1 Answers1

1

Somewhat counter-intuitively, it's actually image #2 that is displaying correctly and image #1 that is broken.

Both images have an EXIF orientation value of 6, meaning the image is rotated 90 degrees counter-clockwise. (Refer to this excellent post about EXIF orientation for more detail.)

imagecreatefromjpeg doesn't transform images in any way. It is loading the image 'raw'. It only looks like it's rotated because your default viewer (Windows thumbnail/Windows Photo Viewer?) is automatically doing this based on the EXIF value. Jeffrey's Exif Viewer shows the raw image as well, that's why your results are consistent.

According to that value, to display both images correctly you should perform a 90 degree clockwise rotation transform:

$img = imagerotate($img, -90, 0);

This gives the following result (images scaled down here):

Image #1:

enter image description here

Image #2:

enter image description here

Which is exactly what I see for both images in Win8 photo viewer. So because image #1 has the wrong orientation value there is no way to rotate it correctly via code.

timclutton
  • 12,682
  • 3
  • 33
  • 43