【vue】上传图片旋转问题

【记录一下项目中遇到的问题。】

问题:

做上传图片的功能时, 可以选择文件、可以选择拍照上传,发现安卓手机上传的图片出现转向问题。

解决

获取图片的方向信息, 做旋转后再进行上传。

1、安装、引用exif-js
npm i exif-js -S

import EXIF from 'exif-js';
let img = new Image();
img.src = e.target.result;

EXIF.getData(img, () => {
    let orientation = EXIF.getTag(img, 'Orientation');
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    //判断是苹果ios还是安卓
    let isIos = navigator.userAgent.match(/ipad|iphone/i);
    if (isIos) {
        orientation = '';// ios: 无旋转问题
    }
    var originWidth = img.width, originHeight = img.height;
    // 最大尺寸限制
    var maxWidth = 1000, maxHeight = 1000;
    // 目标尺寸
    var targetWidth = originWidth, targetHeight = originHeight;

    switch (orientation) {
        case 3:
            canvas.width = targetWidth;
            canvas.height = targetHeight;
            ctx.rotate(Math.PI);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, -targetWidth, -targetHeight, targetWidth, targetHeight);// 图片压缩
            break;
        case 6:
            canvas.width = targetHeight;
            canvas.height = targetWidth;
            ctx.rotate(0.5 * Math.PI);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, 0, -targetHeight, targetWidth, targetHeight);// 图片压缩
            break;
        case 8:
            canvas.width = targetHeight;
            canvas.height = targetWidth;
            ctx.rotate((3 * Math.PI) / 2);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, -targetWidth, 0, targetWidth, targetHeight);// 图片压缩
            break;
        default:
            canvas.width = targetWidth;
            canvas.height = targetHeight;
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, 0, 0, targetWidth, targetHeight);// 图片压缩
            break;
    }

    let ndata = canvas.toDataURL("image/jpeg", 0.7);

旋转、压缩代码:

EXIF.getData(img, () => {
    let orientation = EXIF.getTag(img, 'Orientation');
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    //判断是苹果ios还是安卓
    let isIos = navigator.userAgent.match(/ipad|iphone/i);
    if (isIos) {
        orientation = '';// ios: 无旋转问题
    }
    var originWidth = img.width, originHeight = img.height;
    // 最大尺寸限制
    var maxWidth = 1000, maxHeight = 1000;
    // 目标尺寸
    var targetWidth = originWidth, targetHeight = originHeight;
    // 图片尺寸超过限制
    if (originWidth > maxWidth || originHeight > maxHeight) {
        if (originWidth / originHeight > maxWidth / maxHeight) {
            // 更宽,按照宽度限定尺寸
            targetWidth = maxWidth;
            targetHeight = Math.round(
                maxWidth * (originHeight / originWidth)
            );
        } else {
            targetHeight = maxHeight;
            targetWidth = Math.round(
                maxHeight * (originWidth / originHeight)
            );
        }
    }
    switch (orientation) {
        case 3:
            canvas.width = targetWidth;
            canvas.height = targetHeight;
            ctx.rotate(Math.PI);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, -targetWidth, -targetHeight, targetWidth, targetHeight);// 图片压缩
            break;
        case 6:
            canvas.width = targetHeight;
            canvas.height = targetWidth;
            ctx.rotate(0.5 * Math.PI);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, 0, -targetHeight, targetWidth, targetHeight);// 图片压缩
            break;
        case 8:
            canvas.width = targetHeight;
            canvas.height = targetWidth;
            ctx.rotate((3 * Math.PI) / 2);
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, -targetWidth, 0, targetWidth, targetHeight);// 图片压缩
            break;
        default:
            canvas.width = targetWidth;
            canvas.height = targetHeight;
            ctx.clearRect(0, 0, targetWidth, targetHeight);// 清除画布
            ctx.drawImage(img, 0, 0, targetWidth, targetHeight);// 图片压缩
            break;
    }

    let ndata = canvas.toDataURL("image/jpeg", 0.7);
   
    let time = new Date().getTime();
    if (viewRef === "img1") {
        that.base64img1 = ndata;
        that.idcardFront = CommonJS.base64toFile(ndata, "idcardFront" + time + ".png");
        that.fileUpload(that.idcardFront, "idcardFront");
    }
    if (viewRef === "img2") {
        that.base64img2 = ndata;
        that.idcardBack = CommonJS.base64toFile(ndata, "idcardBack" + time + ".png");
        that.fileUpload(that.idcardBack, "idcardBack");
    }
    if (viewRef === "img3") {
        that.base64img3 = ndata;
        that.idcardInHand = CommonJS.base64toFile(ndata, "idcardInHand" + time + ".png");
        that.fileUpload(that.idcardInHand, "idcardInHand");
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于如何解决vue项目中上传图片旋转的问题,可以考虑使用exif-js库来读取图片的Exif信息,并根据旋转角度进行旋转处理,最后再上传图片。具体代码可以参考下面的示例: ``` // 安装 exif-js 库 npm install exif-js --save // 引入 exif-js 库 import EXIF from 'exif-js'; // 假设选择的图片为 file 对象 const file = ...; // 读取图片的 Exif 信息 EXIF.getData(file, function() { // 获取旋转角度信息 const orientation = EXIF.getTag(this, "Orientation"); // 根据旋转角度进行旋转处理 let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); let img = document.createElement('img'); img.onload = function() { if ([5,6,7,8].indexOf(orientation) > -1) { canvas.width = img.height; canvas.height = img.width; } else { canvas.width = img.width; canvas.height = img.height; } switch (orientation) { case 2: ctx.transform(-1, 0, 0, 1, img.width, 0); break; case 3: ctx.transform(-1, 0, 0, -1, img.width, img.height); break; case 4: ctx.transform(1, 0, 0, -1, 0, img.height); break; case 5: ctx.transform(0, 1, 1, 0, 0, 0); break; case 6: ctx.transform(0, 1, -1, 0, img.height, 0); break; case 7: ctx.transform(0, -1, -1, 0, img.height, img.width); break; case 8: ctx.transform(0, -1, 1, 0, 0, img.width); break; default: ctx.transform(1, 0, 0, 1, 0, 0); } ctx.drawImage(img, 0, 0); const rotatedDataUrl = canvas.toDataURL('image/jpeg'); // 上传旋转后的图片 // ... }; img.src = URL.createObjectURL(file); }); ``` 上述代码中,通过使用exif-js库获取图片的旋转角度信息,利用canvas进行旋转处理,最后上传旋转后的图片。 希望可以帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值