9

I use a PHP script that uploads an image, then gets the dimensions with PHP's getImageSize() and then does things to the image according to the pictures orientation (portrait or landscape).

However (PHP version 5.4.12) on some .jpg files it gets the height and width as they are, and in some (taken with an iPhone) it swaps them, thinking the portrait pictures are actually landscape.
It does not only happen on my local Wampserver, but also on a remote server (with a different PHP version).

Has anyone a clue how

1) to repair this or
2) find a way around the problem?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Chiel
  • 91
  • 1
  • 2
  • How are you working with the result of getimagesize? You always get a larger and a smaller value (except for square images) and should work accordingly. Show us your code, that gives the problem – Edwin Krause Mar 27 '15 at 13:41
  • I have a same issue. I have Wamp with PHP 5.5. It looks like a bug in build-in PHP interpret. – Daniel.P. Sep 24 '15 at 11:21
  • Anyone found a solution for this? I have exact same problem – MeV Mar 17 '16 at 19:00
  • @EdwinKrause what do you mean "you always get a larger and a smaller value"? the first value should always be the width – MeV Mar 17 '16 at 19:00

1 Answers1

9

Some cameras include an orientation tag within the metadata section of the file itself. This is so the device itself can show it in the correct orientation every time regardless of the picture's orientation in its raw data.

It seems like Windows doesn't support reading this orientation tag and instead just reads the pixel data and displays it as-is.

A solution would be to either change the orientation tag in afflicted pictures' metadata on a per-image basis, OR

Use PHP's exif_read_data() function to read the orientation and orient your image accordingly like so:

<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}
// $image now contains a resource with the image oriented correctly
?>

References:

Community
  • 1
  • 1
Pablo Canseco
  • 559
  • 1
  • 9
  • 24
  • We are talking about the width/height not about the orientation. no? – MeV Mar 17 '16 at 19:11
  • The image orientation being wrong would result in width/height getting mixed up. – Pablo Canseco Mar 17 '16 at 19:17
  • Thanks for the reply. I have checked the Image EXIF but the image orientation is 1, therefore it doesnt seems to be wrong to me. – MeV Mar 17 '16 at 19:25
  • Thanks very much! When photo's from MY phone are taken portrait, getImageSize mixes up the width and the height. When I use photo's from my daugthers phone on portrait mode, it doesn't. So I display the orientation, and all of my photos say '6', and her photo's all say '1'. I'm not sure what it means, but now I know when to switch the values back! – Michel May 16 '16 at 21:33