1 回答

TA貢獻1853條經驗 獲得超6個贊
我使用以下代碼解決了這個問題。它將從 EXIF 數據中檢測方向,并根據方向值旋轉它。這是在上傳圖像時完成的。
move_uploaded_file($uploadedFile, $destinationFilename);
correctImageOrientation($destinationFilename);
function correctImageOrientation($filename) {
? if (function_exists('exif_read_data')) {
? ? $exif = exif_read_data($filename);
? ? if($exif && isset($exif['Orientation'])) {
? ? ? $orientation = $exif['Orientation'];
? ? ? if($orientation != 1){
? ? ? ? $img = imagecreatefromjpeg($filename);
? ? ? ? $deg = 0;
? ? ? ? switch ($orientation) {
? ? ? ? ? case 3:
? ? ? ? ? ? $deg = 180;
? ? ? ? ? ? break;
? ? ? ? ? case 6:
? ? ? ? ? ? $deg = 270;
? ? ? ? ? ? break;
? ? ? ? ? case 8:
? ? ? ? ? ? $deg = 90;
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if ($deg) {
? ? ? ? ? $img = imagerotate($img, $deg, 0);? ? ? ??
? ? ? ? }
? ? ? ? // then rewrite the rotated image back to the disk as $filename?
? ? ? ? imagejpeg($img, $filename, 95);
? ? ? } // if there is some rotation necessary
? ? } // if have the exif orientation info
? } // if function exists? ? ??
}
- 1 回答
- 0 關注
- 350 瀏覽
添加回答
舉報