解决上传图片自动旋转的问题以及对图片进行压缩上传

1. 关于图片自动旋转原因

在使用PS或者其他软件旋转图片时,图片旋转了,但Orientation不会改变,由于我们使用的图片预览器能够预处理图片,使其看起来与旋转后一致,但上传图片时,浏览器并不会预处理。所以导致图片旋转。

EXIF中,包含一个Orientation参数,用来记录拍摄照片时的方向,其中1是正常。

å¾çæè¿°

2. 解决旋转问题,需引入exif.js文件    下载链接 Exif.js 读取图像的元数据 - 前端开发仓库

顺带对图片进行压缩上传

// 1.图片旋转及图片压缩  fileObj指input上传的图片文件,callback会将base64图片传入,自行处理显示
function compress(fileObj,callback) {  	
    if(typeof (FileReader) === 'undefined'){
        console.log("当前浏览器内核不支持base64图片压缩")
        // getObjectURL()方法见下方,无法压缩图片时直接将fileObj处理为网页可显示的图片
        callback(getObjectURL(fileObj))     
    }else{
        try{
            var reader = new FileReader();
            reader.onload = function (e) {
                var image = new Image();
                image.onload = function () {
                    var Orientation
                    EXIF.getData(image, function() {
						Orientation = EXIF.getTag(this, 'Orientation');
					});
						
					var canvas = document.createElement('canvas'),
						context = canvas.getContext('2d'),
						squareW = this.width, //定义画布大小,也就是图片压缩之后的像素
                        squareH = this.height;
							
					if(Orientation == 3) {
						canvas.width = squareW;
						canvas.height = squareH;
						context.rotate(Math.PI);
						context.drawImage(image, 0, 0, -squareW, -squareH);
	
					} else if(Orientation == 8) {
						canvas.width = squareH;
						canvas.height = squareW;
						context.rotate(Math.PI * 3 / 2);
						context.drawImage(image, 0, 0, -squareW, squareH);
							
					} else if(Orientation == 6) {
						canvas.width = squareH;
						canvas.height = squareW;
						context.rotate(Math.PI / 2);
						context.drawImage(image, 0, 0, squareW, -squareH);
	
					} else {
						canvas.width = squareW;
						canvas.height = squareH;
						context.drawImage(image, 0, 0, squareW, squareH);

					}

                    var data = canvas.toDataURL('image/jpeg')
                    callback(data)
                }
                    
                image.src = e.target.result
            };
            reader.readAsDataURL(fileObj);
        }catch (e) {
            console.log('压缩失败!')
            callback(getObjectURL(fileObj))
        }
    }
}
//  getObjectURL()  将file类型转化为可显示的图片
function getObjectURL(file) {
	    var url = null ;
	    if (window.createObjectURL!=undefined) { // basic
	        url = window.createObjectURL(file) ;
	    } else if (window.URL!=undefined) { // mozilla(firefox)
	        url = window.URL.createObjectURL(file) ;
	    } else if (window.webkitURL!=undefined) { // webkit or chrome
	        url = window.webkitURL.createObjectURL(file) ;
	    }
	    return url;
	}

//base64转file  dataurl是base64地址,filename是图片的名字
function dataURLtoFile(dataurl, filename) { 
	    var arr = dataurl.split(','), 
	        mime = arr[0].match(/:(.*?);/)[1],
	        bstr = atob(arr[1]), 
	        n = bstr.length, 
	        u8arr = new Uint8Array(n);
	    while(n--){
	        u8arr[n] = bstr.charCodeAt(n);
	    }
	    return new File([u8arr], filename, {type:mime});
	}
//base64转blob  dataurl是base64地址
	function dataURLtoBlob(dataurl) {
	    var arr = dataurl.split(','), 
	        mime = arr[0].match(/:(.*?);/)[1],
	        bstr = atob(arr[1]), 
	        n = bstr.length, 
	        u8arr = new Uint8Array(n);
	    while(n--){
	        u8arr[n] = bstr.charCodeAt(n);
	    }
	    return new Blob([u8arr], {type:mime});
	}

  • 24
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值