图像在移动设备上正确旋转,而不在桌面上旋转 [英] Image is rotated correctly on mobile device, not on desktop

查看:83
本文介绍了图像在移动设备上正确旋转,而不在桌面上旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传的图片出现奇怪的问题.当我在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);

如果您知道为什么它只能在某些平台上正确旋转,我将不胜感激!

应该澄清一下,图片最常会从智能手机或平板电脑上上传.

解决方案

有些错误使代码无法正常工作.尝试打开错误报告,以帮助您调试此类问题.

  • exif_read_data()适用于文件而不是GD资源,因此请传递$filepath而不是$image.
  • imageflip()直接操作资源并返回bool,因此将返回值分配给$image会破坏资源.
  • 根本不需要第二个switch()语句. imagecreatefrom___()函数从文件创建资源,但是您要向它们传递已经创建的资源-您要做的就是输出它.

否则,方向校正似乎是准确的,并且应该对您有用(它适用于我用手机拍摄的各种测试照片).

这是更正的代码:

$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));

// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 1: // nothing
            break;
        case 2: // horizontal flip
            imageflip($image, IMG_FLIP_HORIZONTAL);
            break;
        case 3: // 180 rotate left
            $image = imagerotate($image, 180, 0);
            break;
        case 4: // vertical flip
            imageflip($image, IMG_FLIP_VERTICAL);
            break;
        case 5: // vertical flip + 90 rotate right
            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
            imageflip($image, IMG_FLIP_HORIZONTAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 8:    // 90 rotate left
            $image = imagerotate($image, 90, 0);
            break;
    }
}

imagejpeg($image, $_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);

I'm having a weird issue with my uploaded images. They're rotated correctly when I view them on iPhones and iPads, but whenever I try to view them on desktop, they're displayed with their wrong orientation. I can't find the error, and after spending hours messing with the EXIF data I'm close to giving up. After fixing the orientation, I'm also resizing the images, but that shouldn't interfere with the other code. In case it does, I'm including it.

I don't have enough reputation to upload images, but here's a link to them:
http://i.imgur.com/ARwSUuV.png
http://i.imgur.com/00yj7OJ.png

Here's the code I'm using to upload:

$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);

If you have any idea why it's only rotating correctly on some platforms, I'd be very grateful!

EDIT: Should probably clarify that images will most often be uploaded from smartphones or tablets.

解决方案

There are some errors that stop the code working. Try turning on error reporting to help you debug problems like this.

  • exif_read_data() works on a file, not a GD resource, so pass $filepath instead of $image.
  • imageflip() manipulates the resource directly and returns a bool so assigning the return value to $image destroys the resource.
  • The second switch() statement isn't needed at all. The imagecreatefrom___() functions create a resource from a file, but you're passing them an already created resource - all you want to do is output it.

Otherwise the orientation correction seems accurate and should work for you (it does on the various test photos I took with my phone).

Here's the corrected code:

$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));

// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 1: // nothing
            break;
        case 2: // horizontal flip
            imageflip($image, IMG_FLIP_HORIZONTAL);
            break;
        case 3: // 180 rotate left
            $image = imagerotate($image, 180, 0);
            break;
        case 4: // vertical flip
            imageflip($image, IMG_FLIP_VERTICAL);
            break;
        case 5: // vertical flip + 90 rotate right
            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
            imageflip($image, IMG_FLIP_HORIZONTAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 8:    // 90 rotate left
            $image = imagerotate($image, 90, 0);
            break;
    }
}

imagejpeg($image, $_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);

这篇关于图像在移动设备上正确旋转,而不在桌面上旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆