加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在使用PHP上传图像之前检查/修复图像旋转

发布时间:2020-12-13 13:36:46 所属栏目:PHP教程 来源:网络整理
导读:上传使用 iphone拍摄的图像时,我有一个问题. 我试图使用 PHP和imgrotate函数自动决定是否需要将图像旋转到正确的位置,然后再将其上传到服务器. 我的HTML代码: form class="form-horizontal" method="post" enctype="multipart/form-data" div class="form-g
上传使用 iphone拍摄的图像时,我有一个问题.
我试图使用 PHP和imgrotate函数自动决定是否需要将图像旋转到正确的位置,然后再将其上传到服务器.

我的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是资源,字符串在.

任何人都有这个场景的工作代码?

<?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错误.在传递文件名时,它希望第一个参数是资源.检查 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,0);
        break;
        case 8:
        $image = imagerotate($imageResource,0);
        break;
        default:
        $image = $imageResource;
    } 
}

imagejpeg($image,90);

?>

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读