解决ios图片使用vue + elementUI上传旋转问题

本文介绍了一种解决iOS设备拍摄的照片在PC端显示时旋转90度的问题。通过在Element UI的上传组件中添加图片预处理逻辑,利用exif插件读取并判断图片方向,再对特定方向的图片进行旋转,确保上传后的图片正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目快上线了。测试小姐姐突然找我,提出ios手机竖拍的照片会在pc上显示会逆时针旋转90度。

项目中使用element upload实现上传,在此记录下解决的方法。

<el-upload  ref="uploadSgs" :action="this.baseServerUrl+'/fileUpload/uploadPic?filepath=designFile'"  
            :on-success="handleSgsSuccess" :on-preview="handleSgsPreview" :beforeUpload="beforeAvatarUpload">
            <el-button size="small" type="primary"></el-button>
</el-upload>

在:beforeUpload 中对上传的图片进行处理。

beforeAvatarUpload(file) {
     //校验图片最大上传10M var testmsg=file.name.substring(file.name.lastIndexOf('.')+1); const isLt2M = file.size / 1024 / 1024
< 10; if(!isLt2M) { this.$message({ message: '上传文件大小不能超过 10MB!', type: 'warning' }); return isLt2M; }else {
      //这里beforeUpload方法可以返回一个Promise,我们可以通过resolve将处理过后的文件上传; return new Promise((resolve)
=> { fileUtil.getOrientation(file).then((orient) => { if (orient && orient === 6) { let reader = new FileReader() let img = new Image() reader.onload = (e) => { img.src = e.target.result img.onload = function () { const data = fileUtil.rotateImage(img, img.width, img.height) const newFile = fileUtil.dataURLtoFile(data, file.name) resolve(newFile) } } reader.readAsDataURL(file) } else { resolve(file) } }) }) }
}

这里需要安装exif插件

 

npm install exif-js --save

exif的使用方法看这里:http://code.ciaoca.com/javascript/exif-js/

 

使用到的方法:

import EXIF from 'exif-js'
 
export default {
    getOrientation: (file) => {
        return new Promise((resolve) => {
            EXIF.getData(file, function () {
                const orient = EXIF.getTag(this, 'Orientation')
                resolve(orient)
            })
        })
    },
 
    dataURLtoFile: (dataurl, filename) => {
        const arr = dataurl.split(',')
        const mime = arr[0].match(/:(.*?);/)[1]
        const bstr = atob(arr[1])
        let n = bstr.length
        let u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, {type: mime});
    },
 
    rotateImage: (image, width, height) => {
        let canvas = document.createElement('canvas')
        let ctx = canvas.getContext('2d')
        ctx.save()
        canvas.width = height
        canvas.height = width
        ctx.rotate(90 * Math.PI / 180)
        ctx.drawImage(image, 0, -height)
        ctx.restore()
        return canvas.toDataURL("image/jpeg")
    },
}

 

其他方法:

handlePictureDetailPreview(file) {
        console.log(file)
        const _self = this;
        this.dialogProcessImageUrl = file.url;
        this.dialogProcessVisible = true;
      },
      handleDetailRemove(file, fileList){
        const _self = this;
        _self.imageDetaillist.pop();
      },
      handleDetailSuccess(res,file) {
        console.log(file)
        console.log(res)
        const _self = this;
        let imgList={};
        let lastindex = res.obj.lastIndexOf('/');
        imgList.name=res.obj.substr(lastindex+1);
        imgList.url=urlConfig.imgServerUrl+res.obj;
        _self.imageDetaillist.push(imgList);
        _self.formImageDetail.image=res.obj;
      },

 

参考网站:https://blog.youkuaiyun.com/qq_23158083/article/details/82835357;

 

转载于:https://www.cnblogs.com/ymdzha/p/10113331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值