PHP/IOS6 prvent image from rotating when uploading from iphone using exif

不打扰是莪最后的温柔 提交于 2019-12-24 01:02:14

问题


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.

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.

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.

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...

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.

I have changed this to

move_uploaded_file($image,$target);`

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?


回答1:


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:

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.

Regards.



来源:https://stackoverflow.com/questions/14168400/php-ios6-prvent-image-from-rotating-when-uploading-from-iphone-using-exif

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!