前端解决图片在浏览器中旋转问题

3 篇文章 0 订阅
3 篇文章 0 订阅

在开始之前需要了解为什么在浏览器中图片会自动旋转,在一些相机设备中因手持方向的原因导致图片会有一个旋转角度,在浏览器中旋转角度被忽略了,导致图片看过去是旋转的

图片旋转如下
在这里插入图片描述
如图
1 正常
6 旋转90°
8 旋转-90°
3 旋转180°

既然知道了图片需要旋转的角度,那么只需要通过canvas旋转并绘制图片,然后将图片输出就行,这里需要借助exif-js获取图片的源信息,只需要判断 Orientation 然后旋转对应的角度,废话不多说上代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            padding-left: 300px;
        }
        .preivew {
            width: 300px;
        }

        .preivew2 {
            width: 300px;
        }
    </style>
    <!-- 这里采用cdn形式引入exif -->
    <script src="https://cdn.jsdelivr.net/npm/exif-js"></script>
</head>

<body>
    <input type="file" accept="image/*" />
    <p>转换前</p>
    <img class="preivew" />
    <p>转换后</p>
    <img class="preivew2" />
</body>

</html>

<script>
    // 添加input事件监听
    document.querySelector('input').addEventListener('change', onFileChange)

    function onFileChange(event) {
        let file = event.target.files[0] // 得到file对象
        document.querySelector('.preivew').src = URL.createObjectURL(file) // 转换前的img
        rotateImg(file).then(blob => {
            document.querySelector('.preivew2').src = URL.createObjectURL(blob) // 转换后的img

            // 上传获取到的blob对象
            // let fromData = new fromData()
            // fromData.appendChild('file',blob)
            // $.ajax({
            //     type: "method",
            //     url: "url",
            //     data: "data",
            //     dataType: "dataType",
            //     success: function (response) {
            //     }
            // });

            /*
             *这里把转换后的blob对象下载在电脑上查看,实际开发中忽略
             */
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', 'AAAAAA')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
            window.URL.revokeObjectURL(url)
        })
    }


    function rotateImg(file) {
        return new Promise((resolve, reject) => {
            let img = new Image();
            img.src = window.URL.createObjectURL(file);
            img.onload = () => {
                // 获取图片源数据 上面已经引入EXIF全局变量
                EXIF.getData(img, function () {
                    // 获取图片orientation值
                    console.log(EXIF.getAllTags(this))
                    let orientation = EXIF.getTag(this, "Orientation");
                    let canvas = document.createElement("canvas");
                    let ctx = canvas.getContext("2d");
                    switch (orientation) {
                        case 3: // 旋转180°
                            canvas.width = img.width;
                            canvas.height = img.height;
                            ctx.rotate((180 * Math.PI) / 180);
                            ctx.drawImage(img, -img.width, -img.height, img.width, img.height);
                            break;
                        case 6: // 旋转90°
                            canvas.width = img.height;
                            canvas.height = img.width;
                            ctx.rotate((90 * Math.PI) / 180);
                            ctx.drawImage(img, 0, -img.height, img.width, img.height);
                            break;
                        case 8: // 旋转-90°
                            canvas.width = img.height;
                            canvas.height = img.width;
                            ctx.rotate((-90 * Math.PI) / 180);
                            ctx.drawImage(img, -img.width, 0, img.width, img.height);
                            break;
                        default: // 没有源信息的图片和正常的图片是不需要旋转的
                            canvas.width = img.width;
                            canvas.height = img.height;
                            ctx.drawImage(img, 0, 0, img.width, img.height);
                            break;
                    }
                    // 处理完返回 (这里返回都是被压缩的,根据实际情况更改正常的图片处理方式)
                    canvas.toBlob(file => resolve(file), 'image/jpeg', 0.92)
                })
            }
        })
    }
</script>

看看效果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值