Skip to content

Instantly share code, notes, and snippets.

@shabbirbhimani
Created November 17, 2019 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabbirbhimani/03933c49e7f9d9b7515ee2fc96cd32cd to your computer and use it in GitHub Desktop.
Save shabbirbhimani/03933c49e7f9d9b7515ee2fc96cd32cd to your computer and use it in GitHub Desktop.
<?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