本文介绍了如何在使用PHP上传图片之前检查/修复图片旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传使用iphone拍摄的图像时出现问题.我正在尝试使用PHP和imgrotate函数自动确定是否需要将图像旋转到正确的位置,然后再将其上传到服务器.

I have a issiue when uploading a image taken using iphone.I am trying to use PHP and the imgrotate function to automaticly decide if the image needs to be rotated to the correct possition, before uploading it to the server.

我的html代码:

<form class="form-horizontal" method="post" enctype="multipart/form-data">
     <div class="form-group">
        <div class="col-md-9">
            <div class="input-group">
                <span class="input-group-btn">
                    <span class="btn btn-default btn-file">
                        Choose img<input type="file" name="file" id="imgInp">
                    </span>
                </span>

            </div>
        </div>
    </div>
    <button type="submit">Send</button>
</form>

使用以下代码的PHP代码:还返回错误:警告:imagerotate()期望参数1为资源,输入字符串.

The PHP code im using:Also return a error: Warning: imagerotate() expects parameter 1 to be resource, string given in.

有人针对这种情况有工作代码吗?

Anyone have a working code for this scenario?

<?php

$filename = $_FILES['file']['name'];
$exif = exif_read_data($_FILES['file']['tmp_name']);


 if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($filename, -180, 0);
                break;
            case 6:
                $image = imagerotate($filename, 90, 0);
                break;
            case 8:
                $image = imagerotate($filename, -90, 0);
                break;
        }
    }

    imagejpeg($image, $filename, 90);

?>

推荐答案

您使用的imagerotate错误.在传递文件名时,它期望第一个参数成为资源.检查手册

You are using imagerotate wrong. It expect first parameter to be resource while you are passing the filename. Check manual

尝试一下:

<?php

$filename = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];
$exif = exif_read_data($_FILES['file']['tmp_name']);
if (!empty($exif['Orientation'])) {
    $imageResource = imagecreatefromjpeg($filePath); // provided that the image is jpeg. Use relevant function otherwise
    switch ($exif['Orientation']) {
        case 3:
        $image = imagerotate($imageResource, 180, 0);
        break;
        case 6:
        $image = imagerotate($imageResource, -90, 0);
        break;
        case 8:
        $image = imagerotate($imageResource, 90, 0);
        break;
        default:
        $image = $imageResource;
    }
}

imagejpeg($image, $filename, 90);

?>

不要忘记添加以下两行来释放内存:

Do not forget to free the memory by adding following two lines:

imagedestroy($imageResource);
imagedestroy($image);

这篇关于如何在使用PHP上传图片之前检查/修复图片旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:38