使用exif从iphone上传时,PHP / IOS6防止旋转图像 [英] PHP/IOS6 prvent image from rotating when uploading from iphone using exif

查看:149
本文介绍了使用exif从iphone上传时,PHP / IOS6防止旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用PHP编写的照片上传脚本,可以在普通的PC图像上正常工作,但是,当上传在iPhone上拍摄的图像时,它会旋转90度。显然,问题在于iphone作为一个较新的相机对图像进行编码,其属性包括最新标准中规定的方向,我需要使用exif数据进行校正。我在PHP手册中找到了两个使用exif_read_data函数的脚本($ _ FILES ['file'] ['name']);收集方向数据,然后进行必要的调整以正确定位它们。但是,我无法让它们工作。

I have a photo upload script written in PHP that works fine on regular pc images, however, when uploading images snapped on an iphone, it rotates them 90 degrees. Apparently, the problem is that the iphone as a newer camera encodes the image with attributes including orientation spelled out in the latest standard and I need to use exif data to correct for this. I have found two scripts in the PHP manual that use the function exif_read_data($_FILES['file']['name']); to collect the orientation data and then make the necessary adjustements to orient them properly. However, I cannot get them to work.

首先,我收到一条错误,指出exif_read_data函数无效,即使手册说它自PHP 4.2起有效我跑了5.2。

First, I am getting an error that says the exif_read_data funcion is invalid even though the manual says it is valid as of PHP 4.2 and I am runnign 5.2.

其次,我不清楚这些脚本中$ image的真正含义。以前,我只是使用 move_uploaded_file($ _ FILES [file] [tmp_name],$ target); 来有效上传文件。

Second, it is unclear to me what $image really means in these scripts. Previously, I just used move_uploaded_file($_FILES["file"]["tmp_name"],$target); to effectively upload the file.

现在我应该上传 $ image $ _ FILES 中的文件>但我认为这可能与 $ _ FILES [file] [tmp_name] 不一样。它可能是从文件创建的字符串,但最后当天,我真的不知道$ image是什么...

Now instead of the file in $_FILES, I am supposed to be uploading $image but I think this may not be the same thing as $_FILES["file"]["tmp_name"] It may be a string created from the file but at the end of the day, I don't really know what $image is...

以下是PHP手册的功能....

Here are the functions fro the PHP manual....

1)

  $exif = exif_read_data($_FILES['file']['name']);
        $ort = $exif['IFD0']['Orientation'];
            switch($ort)
            {
                case 1: 

// nothing
        break;

        case 2: // horizontal flip
            $image->flipImage($public,1);
        break;

        case 3: // 180 rotate left
            $image->rotateImage($public,180);
        break;

        case 4: // vertical flip
            $image->flipImage($public,2);
        break;

        case 5: // vertical flip + 90 rotate right
            $image->flipImage($public, 2);
                $image->rotateImage($public, -90);
        break;

        case 6: // 90 rotate right
            $image->rotateImage($public, -90);
        break;

        case 7: // horizontal flip + 90 rotate right
            $image->flipImage($public,1);   
            $image->rotateImage($public, -90);
        break;

        case 8:    // 90 rotate left
            $image->rotateImage($public, 90);
        break;
    }

2)

  $image = imagecreatefromstring(file_get_contents($_FILES['file']['name']));
    $exif = exif_read_data($_FILES['file']['name']);
    if(!empty($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;
        }
    }

在遇到此问题之前,我刚刚使用

Prior to encountering this problem, I just used

 move_uploaded_file($_FILES["file"]["tmp_name"],$target); to upload file.

我已将此更改为

move_uploaded_file($image,$target);`

当我跑步时这个,它抛出 exif_read_data 不是一个有效的函数错误,还说 file_get_contents 和我 magecreatefromstring 不是有效的函数。

When I run this, it is throwing the exif_read_data is not a valid function error and also says file_get_contents and imagecreatefromstring are not valid functions.

有没有人成功解决过这个问题?

Has anyone successfully solved this issue?

推荐答案

我认为它有点晚了,但我有一个功能脚本使用它,唯一的事情是我的脚本适用于改变大小而不保存图像。

I think it is a little bit late, but I have a functional script working with this, the only thing is that my script works for change the size on fly not to save the image.

无论如何,这是你的解决方案:

Anyway, here's your solution:

首先,像往常一样使用move_uploaded_file保存你的文件,稍后运行此代码:

First of all, save your file as usual, using move_uploaded_file, later run this code:

<?
$buffer = ImageCreateFromJPEG($target);
$exif = exif_read_data($_GET['img']);
if(!empty($exif['Orientation'])){
    switch($exif['Orientation']){
        case 8:
            $buffer = imagerotate($buffer,90,0);
        break;
        case 3:
            $buffer = imagerotate($buffer,180,0);
        break;
        case 6:
            $buffer = imagerotate($buffer,-90,0);
        break;
    }
}

imagejpeg($buffer, $target, 90);
?>

这应该可以解决您的问题。

And that should solve your problem.

问候。

这篇关于使用exif从iphone上传时,PHP / IOS6防止旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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