本文給大家介紹使用php技術實現根據上傳圖片orientation屬性判斷是否需要旋轉,感興趣的朋友一起看看吧
當使用蘋果的iOS系統拍照上傳圖片的時候,可能會遇到圖片被旋轉的問題,這主要是取決于你拍照時拍照按鈕的位置。假設拍照時你把手機旋轉過來底部朝上,那拍出來的照片也是被旋轉了的。
下面的代碼將確保所有上傳的照片在上傳時都能是正確定向:
- <?php
- $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
- $exif = exif_read_data($_FILES['image_upload']['tmp_name']);
- if(!emptyempty($exif['Orientation'])) {
- switch($exif['Orientation']) {
- case 8:
- $image = imagerotate($image,90,0);
- break;
- case 3:
- $image = imagerotate($image,180,0);
- break;
- case 6:
- $image = imagerotate($image,-90,0);
- break;
- }
- }
- // $image now contains a resource with the image oriented correctly
- ?>
經測試,Android拍照的 Orientation 屬性都是1,判斷不出是否被旋轉了。
新聞熱點
疑難解答