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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在使用PHP上传图像之前检查/修复图像旋转脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
上传使用 iphone拍摄的图像时,我有一个问题.
我试图使用 PHPimgrotate函数自动决定是否需要将图像旋转到正确的位置,然后再将其上传服务器.

我的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);

脚本宝典总结

以上是脚本宝典为你收集整理的如何在使用PHP上传图像之前检查/修复图像旋转全部内容,希望文章能够帮你解决如何在使用PHP上传图像之前检查/修复图像旋转所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。