Cheat sheet.
Vertical photos shot in portrait mode on Android and iPhone are saved as horizontal images, but the photo orientation is written into EXIF.
If you output the value of this command:
$exif = exif_read_data( $existingFilePath, 0, true);
Then among the array values you will see:
array
(
...
[IFD0] => Array
(
[Make] => Sony
[Model] => LT25i
[Orientation] => 6
[XResolution] => 72/1
[YResolution] => 72/1
[ResolutionUnit] => 2
[Software] => 9.1.A.1.145_58_f100
[DateTime] => 2013:07:26 17:00:01
[YCbCrPositioning] => 1
[Exif_IFD_Pointer] => 214
[GPS_IFD_Pointer] => 626
)
...
)
In this case it means that when this photo is displayed, the app showing it must rotate the image by 90 degrees because the photo is in portrait orientation.
To avoid problems when uploading images to the server and processing them (creating a reduced copy, creating a thumbnail), you can detect such photos with this code:
<?php
imagecopyresampled( $resultImage, $sourceImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$exif = exif_read_data( $existingFilePath, 0, true);
if( false === empty( $exif['IFD0']['Orientation'] ) ) {
switch( $exif['IFD0']['Orientation'] ) {
case 8:
$resultImage = imagerotate( $resultImage, 90, 0 );
break;
case 3:
$resultImage = imagerotate( $resultImage,180,0);
break;
case 6:
$resultImage = imagerotate( $resultImage,-90,0);
break;
}
}
Interestingly, Windows Phone saves a portrait photo as a vertical image.