0

I've created a web app where users can upload their profile picture. If user captures image using his mobile's camera, the orientation gets changed. I managed to resolve it using the following code:

                 $path[0] = $_FILES['image_upload_file']['tmp_name'];

                 $exif = exif_read_data($path[0]);
                 // $exif['Orientation'] = 6;

                 if(isset($exif['Orientation'])){

                    $image = imagecreatefromjpeg($path[0]);
                    file_put_contents("img_data.txt",print_r($image,true));

                    $ort = $exif['Orientation'];
                    switch($ort)
                    {

                        case 3: // 180 rotate left
                            $image = imagerotate($image, 180,0);
                            break;


                        case 6: // 90 rotate right
                             file_put_contents("img_before.txt",print_r($exif,true));
                            $image = imagerotate($image, -90, 0);
                             file_put_contents("img_after.txt",print_r($ort,true));
                            break;

                        case 8:    // 90 rotate left
                            $image = imagerotate($image, 90, 0);
                            break;
                    }
                    imagejpeg($image, $path[0], 90);
                }

Got this code from PHP read_exif_data and Adjust Orientation

However, it only works when image is captured from Android device or from computer. It does not work if image is captured from iPhone device.

while debugging, i've noticed that $image = imagerotate($image, -90, 0); has some issue with iphone. I can say this surely because this line was not executed (as img_after.txt is not created where it was supposed to be created).

file_put_contents("img_after.txt",print_r($ort,true));

Please suggest!

UPDATE:

exif data read from iPhone:

Array
(
    [FileName] => phpYBaC5W
    [FileDateTime] => 1467207697
    [FileSize] => 1430214
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, EXIF
    [COMPUTED] => Array
        (
            [html] => width="3264" height="2448"
            [Height] => 2448
            [Width] => 3264
            [IsColor] => 1
            [ByteOrderMotorola] => 1
        )

    [Orientation] => 6
    [Exif_IFD_Pointer] => 38
    [ColorSpace] => 1
    [ExifImageWidth] => 3264
    [ExifImageLength] => 2448
)

exif data read from Android:

Array
(
    [FileName] => phpMQHUgW
    [FileDateTime] => 1467207789
    [FileSize] => 1842753
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
    [COMPUTED] => Array
        (
            [html] => width="3264" height="1836"
            [Height] => 1836
            [Width] => 3264
            [IsColor] => 1
            [ByteOrderMotorola] => 0
            [ApertureFNumber] => f/2.4
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
            [Thumbnail.Height] => 288
            [Thumbnail.Width] => 512
        )

    [ImageWidth] => 3264
    [ImageLength] => 1836
    [Make] => SAMSUNG
    [Model] => SM-G7102
    [Orientation] => 6
    [XResolution] => 72/1
    [YResolution] => 72/1
    [ResolutionUnit] => 2
    [Software] => G7102DDUBOB1
    [DateTime] => 2016:06:29 19:12:59
    [YCbCrPositioning] => 1
    [Exif_IFD_Pointer] => 238
    [GPS_IFD_Pointer] => 870
    [THUMBNAIL] => Array
        (
            [ImageWidth] => 512
            [ImageLength] => 288
            [Compression] => 6
            [Orientation] => 6
            [XResolution] => 72/1
            [YResolution] => 72/1
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 1018
            [JPEGInterchangeFormatLength] => 5829
        )

    [ExposureTime] => 1/17
    [FNumber] => 240/100
    [ExposureProgram] => 2
    [ISOSpeedRatings] => 1000
    [ExifVersion] => 0220
    [DateTimeOriginal] => 2016:06:29 19:12:59
    [DateTimeDigitized] => 2016:06:29 19:12:59
    [ComponentsConfiguration] =>  
    [ShutterSpeedValue] => 405/100
    [ApertureValue] => 252/100
    [BrightnessValue] => -169/100
    [ExposureBiasValue] => 0/10
    [MaxApertureValue] => 253/100
    [MeteringMode] => 2
    [LightSource] => 0
    [Flash] => 0
    [FocalLength] => 293/100
    [MakerNote] =>       0100                      Z   @         P                             
    [FlashPixVersion] => 0100
    [ColorSpace] => 1
    [ExifImageWidth] => 3264
    [ExifImageLength] => 1836
    [InteroperabilityOffset] => 840
    [SensingMethod] => 2
    [SceneType] => 
    [ExposureMode] => 0
    [WhiteBalance] => 0
    [FocalLengthIn35mmFilm] => 31
    [SceneCaptureType] => 0
    [ImageUniqueID] => E08QLGI01CH
    [GPSVersion] =>   
    [InterOperabilityIndex] => R98
    [InterOperabilityVersion] => 0100
)

If I skip the code to rotate image, it works fine in all mobiles including iphone.

Community
  • 1
  • 1
Rakesh Shewale
  • 497
  • 6
  • 22
  • Given that PHP isn't running on the client (i.e. it's not android or ios running it) the question is flawed. Put in some debugging and find what is being sent differently. – Jonnix Jun 29 '16 at 14:00
  • Why would the client OS matter to the server? Are you sure `$exif['Orientation']` is set properly on iPhones to begin with? – apokryfos Jun 29 '16 at 14:00
  • yes. $exif['Orientation'] is set properly when uploaded from iPhone or Andoird – Rakesh Shewale Jun 29 '16 at 14:02
  • To clarify, `img_before.txt` is created but `img_after.txt` is not? In addition, are you sure the image is a JPG? – apokryfos Jun 29 '16 at 14:02
  • @apokryfos, img_before.txt is created. And yes, the image is valid JPG – Rakesh Shewale Jun 29 '16 at 14:05
  • @JonStirling, what you are saying makes sense. I've added exif data in question to understand input from iPhone and from Android – Rakesh Shewale Jun 29 '16 at 14:07

1 Answers1

0

This is a "feature" of the iPhone to protect privacy by stripping the EXIF data from a file, or at least a fair bit of it when uploading an image.

There used to be a hack where you could obtain that data from Javascript and pass it on to your backend, however this appears no longer to be the case.

I am not an iPhone user anymore, but there have been some speculations and reports that it have been fixed in newer versions of iOS, which however still leaves the issue wide open to the fact that a big part of your user base may not be able to supply you with that data.

There was a lengthy thread here on SO you might wanna read: Image upload from iphone strips exif data

Community
  • 1
  • 1
Kalle
  • 383
  • 4
  • 11
  • 1
    I am able to receive the EXIF data from iPhone too.. I've posted the contents as well. The only issue here is with the statement $image = imagerotate($image, -90, 0); Not sure why it does not execute well. As far as i understand correctly, this statement is being executed at server side and $image is also a valid image.. So it is supposed to give correct results, isn't it? – Rakesh Shewale Aug 06 '16 at 06:57