The solution you are asking for is not quite the same as your example. At first I thought you wanted a smart function that detected the text, but it is simpler than that with your example.
You need to look at the EXIF data. Fortunately, I have doe this multiple times. To correct an image that has been rotated, you can use the following function, which I wrote for correcting images taken on tablets/phones but will be displayed on computers. The input must be a filename for a JPG image.
function fix_image($filename, $max_dimension = 0)
{
$exif = exif_read_data($filename, 0, true);
$o = $exif[IFD0][Orientation];
// correct the image rotation
$s = imagecreatefromjpeg($filename);
if ($o == 1) { }
if ($o == 3) { $s = imagerotate($s, 180, 0); }
if ($o == 6) { $s = imagerotate($s, 270, 0); }
if ($o == 8) { $s = imagerotate($s, 90, 0); }
// export the image, rotated properly
imagejpeg($s, $filename, 100);
imagedestroy($s);
if ($max_dimension > 0)
{
$i = getimagesize($filename);
// reopen image for resizing
// Use the known orientation to determine if it is portrait or landscape
if ($i[0] > $i[1])
{
// landscape: make the width $max_dimension, adjust the height accordingly
$new_width = $max_dimension;
$new_height = $i[1] * ($max_dimension/$i[0]);
} else {
// portrait: make the height $max_dimension, adjust the width accordingly
$new_height = $max_dimension;
$new_width = $i[0] * ($max_dimension/$i[1]);
}
$s = imagecreatefromjpeg($filename);
$n = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($n, $s, 0, 0, 0, 0, $new_width, $new_height, $i[0], $i[1]);
imagejpeg($n, $filename, 100);
imagedestroy($n);
imagedestroy($s);
}
}