Use exif_read_data to find out the orientation of the image:
$exif = exif_read_data('image.jpg');
if (isset($exif['Orientation']))
{
switch ($exif['Orientation'])
{
case 3:
// Need to rotate 180 deg
break;
case 6:
// Need to rotate 90 deg clockwise
break;
case 8:
// Need to rotate 90 deg counter clockwise
break;
}
}
You’ll find an explanation of the orientation codes here:
http://www.impulseadventure.com/photo/exif-orientation.html