Created
November 17, 2019 04:40
-
-
Save shabbirbhimani/03933c49e7f9d9b7515ee2fc96cd32cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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