php-gdHow to auto orient image
We can use EXIF data to get image orientation (in case it has this data set):
<?php
$file = 'source.jpg';
$im = imagecreatefromjpeg($file);
$rotation = [0, 0, 0, 180, 0, 0, -90, 0, 90][@exif_read_data($file)['COMPUTED']['Orientation'] ?: 0];
if ( $rotation ) {
  $im = imagerotate($im, $rotation, 0);
}
imagejpeg($im, 'result.jpg');ctrl + c| $filesource image file to auto orient | imagecreatefromjpegcreates GD image object from given PNG image | 
| exif_read_datareturns  | $rotationdefine rotation angle accordingly to image orientation | 
| imagerotaterotates  | imagejpegsave given image to the given  |