Skip to content

Instantly share code, notes, and snippets.

@ety001
Created September 6, 2015 16:44
Show Gist options
  • Save ety001/87cc8699f7b96305194a to your computer and use it in GitHub Desktop.
Save ety001/87cc8699f7b96305194a to your computer and use it in GitHub Desktop.
照片方向调整
<?php
function adjustPicOrientation($full_filename, $type = 'jpeg'){
$exif = exif_read_data($full_filename);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
if($type == 'jpeg'){
$img = imagecreatefromjpeg($full_filename);
} else if($type == 'png') {
$img = imagecreatefrompng($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($type == 'jpeg'){
imagejpeg($img, $full_filename);
} else if($type == 'png') {
imagepng($img, $full_filename);
}
imagedestroy($img);
}
}
return $full_filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment