php 手機上傳照片後 會自動旋轉 方向問題

php 手機上傳照片後 會自動旋轉 方向問題

最近在處理從手機上傳照片或圖片後,發現當手機是用直的拍時,上傳後會變成旋轉90度的照片。
上網爬了一下,國外已經有大神寫出相關函式來處理這個問題。
函式如下:
//上傳圖片旋轉
function image_fix_orientation($path){
$image = imagecreatefromjpeg($path);
$exif = exif_read_data($path);

if (!empty($exif[‘Orientation’])) {
switch ($exif[‘Orientation’]) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $path);
}
}

之後再呼叫函式即可處理旋轉的問題

image_fix_orientation($path);

打完收工

One thought on “php 手機上傳照片後 會自動旋轉 方向問題

henry 發表迴響 取消回覆

你的電子郵件位址並不會被公開。 必要欄位標記為 *