Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
* Source code by WES of Stackoverflow
* https://stackoverflow.com/questions/3657023/how-to-detect-shot-angle-of-photo-and-auto-rotate-for-website-display-like-desk/18919355#18919355
*/
function correctImageOrientation($filename)
{
if (function_exists('exif_read_data'))
{
$exif = exif_read_data($filename);
if ($exif && isset($exif['Orientation']))
{
$orientation = $exif['Orientation'];
if ($orientation != 1)
{
$img = imagecreatefromjpeg($filename);
$deg = 0;
switch ($orientation)
{
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg)
{
$img = imagerotate($img, $deg, 0);
}
// then rewrite the rotated image back to the disk as $filename
imagejpeg($img, $filename, 95);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment