Last active
December 20, 2015 13:09
-
-
Save ahmedmahmoudit/6136307 to your computer and use it in GitHub Desktop.
Remove camera orientation/angle of the apparatus by reading the EXIF information out of the JPEG file
This file contains 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
function remove_orientation($full_filename) | |
{ | |
$exif = exif_read_data($full_filename); | |
if($exif && isset($exif['Orientation'])) { | |
$orientation = $exif['Orientation']; | |
if($orientation != 1){ | |
$img = imagecreatefromjpeg($full_filename); | |
$mirror = false; | |
$deg = 0; | |
switch ($orientation) { | |
case 2: | |
$mirror = true; | |
break; | |
case 3: | |
$deg = 180; | |
break; | |
case 4: | |
$deg = 180; | |
$mirror = true; | |
break; | |
case 5: | |
$deg = 270; | |
$mirror = true; | |
break; | |
case 6: | |
$deg = 270; | |
break; | |
case 7: | |
$deg = 90; | |
$mirror = true; | |
break; | |
case 8: | |
$deg = 90; | |
break; | |
} | |
if ($deg) $img = imagerotate($img, $deg, 0); | |
if ($mirror) $img = _mirrorImage($img); | |
$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename); | |
imagejpeg($img, $full_filename, 95); | |
} | |
} | |
return $full_filename; | |
} | |
function _mirrorImage ( $imgsrc) | |
{ | |
$width = imagesx ( $imgsrc ); | |
$height = imagesy ( $imgsrc ); | |
$src_x = $width -1; | |
$src_y = 0; | |
$src_width = -$width; | |
$src_height = $height; | |
$imgdest = imagecreatetruecolor ( $width, $height ); | |
if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height ) ) | |
{ | |
return $imgdest; | |
} | |
return $imgsrc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment