我上傳的圖像出現奇怪的問題。當我在iPhone和iPad上查看時,它們會正確旋轉,但每當我嘗試在桌面上查看它們時,都會顯示錯誤的方向。 我找不到錯誤,花了幾個小時搞亂了EXIF數據後,我已經放棄了。 修復方向後,我還調整圖像大小,但不應該干擾其他代碼。如果是這樣,我會包括它。圖像在移動設備上正確旋轉,而不是在桌面上
我沒有足夠的聲譽上傳圖片,但這裏是他們的鏈接:
http://i.imgur.com/ARwSUuV.png
http://i.imgur.com/00yj7OJ.png
下面是我用上傳的代碼:
$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));
// Rotate image correctly!
$exif = exif_read_data($image);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']){
case 1: // nothing
break;
case 2: // horizontal flip
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$image = imagerotate($image,180,0);
break;
case 4: // vertical flip
$image = imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image,-90,0);
break;
case 6: // 90 rotate right
$image = imagerotate($image,-90,0);
break;
case 7: // horizontal flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image,-90,0);
break;
case 8: // 90 rotate left
$image = imagerotate($image,90,0);
break;
}
}
switch ($path_parts['extension']) {
case 'gif' :
$im = imagecreatefromgif($image);
break;
case 'jpg' :
$im = imagecreatefromjpeg($image);
break;
case 'png' :
$im = imagecreatefrompng($image);
break;
case 'bmp' :
$im = imagecreatefrombmp($image);
break;
}
if($im){
imagejpeg($im, $_FILES['file']['tmp_name'], 40);
}
$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);
如果你有任何想法,爲什麼它只能在某些平臺上正確旋轉,我會非常感激!
編輯:應該澄清,圖像將經常從智能手機或平板電腦上傳。
也許不直接關係到你陳述的問題,而是'imageflip()''返回bool',不是資源,所以你不應該把它分配給'$ image'然後嘗試'imagerotate()'。 – timclutton
哦。對。謝謝,蒂姆! 我應該使用哪種方法來翻轉圖像? –
只需使用'imageflip()'而不分配返回值。它直接操縱資源。 – timclutton