我上传的图片遇到了奇怪的问题。当我在 iPhone 和 iPad 上查看它们时,它们会正确旋转,但每当我尝试在桌面上查看它们时,它们都会以错误的方向显示。
我找不到错误,在花了几个小时弄乱 EXIF 数据后,我几乎要放弃了。
修复方向后,我还调整了图像的大小,但这不应该干扰其他代码。如果确实如此,我会将其包括在内。
我没有足够的声誉来上传图像,但这里有一个链接:
https://i.stack.imgur.com/gdayA.png https://i.stack.imgur.com/gdayA.png
https://i.stack.imgur.com/RgPrG.png https://i.stack.imgur.com/RgPrG.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);
如果您知道为什么它只能在某些平台上正确旋转,我将非常感激!
编辑:可能应该澄清图像最常从智能手机或平板电脑上传。