0

I have a very strange problem.

On my website there is a file field that will allow the users to upload their profile picture.

It gets uploaded using JQuery and it gets saved using PHP.

If I upload from a PC / MAC / iPhone then there is no problem whatsoever, however if I upload using an Android device the image gets rotated.

The rotation is not even consistent, it could be 90% 180% or 270%, this happens when taking a image or selecting from the Gallery.

Why would this happen? and is there a possible fix?

Kenny
  • 33
  • 8
  • maybe it depends on the orientation of the phone when the picture was taken – Loopo Jan 06 '17 at 11:49
  • Possible duplicate of [Android getting an image from gallery comes rotated](http://stackoverflow.com/questions/31925712/android-getting-an-image-from-gallery-comes-rotated) – Bradley Wilson Jan 06 '17 at 11:50
  • can you please try this http://stackoverflow.com/questions/31925712/android-getting-an-image-from-gallery-comes-rotated – Ganesh Gudghe Jan 06 '17 at 11:50
  • This is not an Android app, so i have no control over the exif data on the device, this is a Simple website @BradleyWilson After searching i have found that Facebook has the same issue, i believe it is something to do with Portrait / Landscape mode. But how do i explain to all my clients that they should only use landscape if using a Android device. :( – Kenny Jan 06 '17 at 11:51
  • I would remove the Android tag in your question to strictly keep it web based/avoid confusion among Android Developers. – Bradley Wilson Jan 06 '17 at 11:52
  • 1
    While it's not an Android app, you can still read the EXIF and rotate the image asn the EXIF is on the image, not on the device. – freedomn-m Jan 06 '17 at 11:53
  • 1
    Possible duplicate of [Android: Bitmaps loaded from gallery are rotated in ImageView](http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview) – freedomn-m Jan 06 '17 at 11:56

1 Answers1

0

This solved the issue

From the PHPDocs

<?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
?>
Kenny
  • 33
  • 8