解决某些机型图片旋转 (exif.js )

安装

NPM:

npm install exif-js --save    

Bower:

bower install exif-js --save

引用

import EXIF from 'exif-js';

封装

const _rotateImg = (imgFile) => {
    return new Promise((resolve, reject) => {
        EXIF.getData(imgFile, function () {
            let exifTags = EXIF.getAllTags(this);
            let reader = new FileReader();
            reader.readAsDataURL(imgFile);
            reader.onload = e => {
                let imgData = e.target.result;
                // 8 表示 顺时针转了90
                // 3 表示 转了 180
                // 6 表示 逆时针转了90
                if (
                    exifTags.Orientation == 8 ||
                    exifTags.Orientation == 3 ||
                    exifTags.Orientation == 6
                ) {
                    //翻转
                    //获取原始图片大小
                    const img = new Image();
                    img.src = imgData;
                    img.onload = function () {
                        let cvs = document.createElement('canvas');
                        let ctx = cvs.getContext('2d');
                        //如果旋转90
                        if (
                            exifTags.Orientation == 8 ||
                            exifTags.Orientation == 6
                        ) {
                            cvs.width = img.height;
                            cvs.height = img.width;
                        } else {
                            cvs.width = img.width;
                            cvs.height = img.height;
                        }
                        if (exifTags.Orientation == 6) {
                            //原图逆时针转了90, 所以要顺时针旋转90
                            ctx.rotate(Math.PI / 180 * 90);
                            ctx.drawImage(
                                img,
                                0,
                                0,
                                img.width,
                                img.height,
                                0,
                                -img.height,
                                img.width,
                                img.height
                            );
                        }
                        if (exifTags.Orientation == 3) {
                            //原图逆时针转了180, 所以顺时针旋转180
                            ctx.rotate(Math.PI / 180 * 180);
                            ctx.drawImage(
                                img,
                                0,
                                0,
                                img.width,
                                img.height,
                                -img.width,
                                -img.height,
                                img.width,
                                img.height
                            );
                        }
                        if (exifTags.Orientation == 8) {
                            //原图顺时针旋转了90, 所以要你时针旋转90
                            ctx.rotate(Math.PI / 180 * -90);
                            ctx.drawImage(
                                img,
                                0,
                                0,
                                img.width,
                                img.height,
                                -img.width,
                                0,
                                img.width,
                                img.height
                            );
                        }
                        resolve(cvs.toDataURL('image/png'));
                    }
                }
                else {
                    resolve(imgData);
                }
            }
        });
    });
}

使用

_rotateImg(file)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值