Here you go, I decided to play since I had 30 spare minutes to kill
https://github.com/leesherwood/Orientation-Fix-PHP
fix_orientation, ( , , , , , , , - )
, , github , heres
<?php
function fix_orientation($fileandpath) {
if(!file_exists($fileandpath))
return false;
$exif = read_exif_data($fileandpath, 'IFD0');
if(!$exif || !is_array($exif))
return false;
$exif = array_change_key_case($exif, CASE_LOWER);
if(!array_key_exists('orientation', $exif))
return false;
$img_res = get_image_resource($fileandpath);
if(is_null($img_res))
return false;
switch($exif['orientation']) {
case 1: return true; break;
case 2:
$final_img = imageflip($img_res, IMG_FLIP_HORIZONTAL);
break;
case 3:
$final_img = imageflip($img_res, IMG_FLIP_VERTICAL);
break;
case 4:
$final_img = imageflip($img_res, IMG_FLIP_BOTH);
break;
case 5:
$final_img = imagerotate($img_res, -90, 0);
$final_img = imageflip($img_res, IMG_FLIP_HORIZONTAL);
break;
case 6:
$final_img = imagerotate($img_res, -90, 0);
break;
case 7:
$final_img = imagerotate($img_res, 90, 0);
$final_img = imageflip($img_res,IMG_FLIP_HORIZONTAL);
break;
case 8:
$final_img = imagerotate($img_res, 90, 0);
break;
}
if(!isset($final_img))
return false;
$parts = explode("/", $fileandpath);
$oldname = array_pop($parts);
$path = implode('/', $parts);
$oldname_parts = explode(".", $oldname);
$ext = array_pop($oldname_parts);
$newname = implode('.', $oldname_parts).'.orig.'.$ext;
rename($fileandpath, $path.'/'.$newname);
$done = save_image_resource($final_img,$fileandpath);
return $done;
}
function get_image_resource($file) {
$img = null;
$p = explode(".", strtolower($file));
$ext = array_pop($p);
switch($ext) {
case "png":
$img = imagecreatefrompng($file);
break;
case "jpg":
case "jpeg":
$img = imagecreatefromjpeg($file);
break;
case "gif":
$img = imagecreatefromgif($file);
break;
}
return $img;
}
function save_image_resource($resource, $location) {
$success = false;
$p = explode(".", strtolower($location));
$ext = array_pop($p);
switch($ext) {
case "png":
$success = imagepng($resource,$location);
break;
case "jpg":
case "jpeg":
$success = imagejpeg($resource,$location);
break;
case "gif":
$success = imagegif($resource,$location);
break;
}
return $success;
}
if(!function_exists('imageflip')) {
define("IMG_FLIP_HORIZONTAL", 1);
define("IMG_FLIP_VERTICAL", 2);
define("IMG_FLIP_BOTH", 3);
function imageflip($resource, $mode) {
if($mode == IMG_FLIP_VERTICAL || $mode == IMG_FLIP_BOTH)
$resource = imagerotate($resource, 180, 0);
if($mode == IMG_FLIP_HORIZONTAL || $mode == IMG_FLIP_BOTH)
$resource = imagerotate($resource, 90, 0);
return $resource;
}
}
?>