1.file表单组件监听change获取file文件
2.创建fileReader 获取base64位图片路由
3.实例一个Image 将base64位图片路由对其进行赋值,创建canvas 通过drawImage方法对其进行等比例缩放复制,实现图片压缩
4.引入 exif-js 模块 获取旋转角度信息,非1的,对canvas进行旋转,重绘,再通过toDataURL 获取旋转矫正后的图片64位
5.调用回调函数进行图片上传
//upload import EXIF from "exif-js"; function rotateImg(img, direction,canvas) { //最小与最大旋转方向,图片旋转4次后回到原方向 var min_step = 0; var max_step = 3; //var img = document.getElementById(pid); if (img == null)return; var height = canvas.height; var width = canvas.width; //var step = img.getAttribute(‘step‘); var step = 2; if (step == null) { step = min_step; } if (direction == ‘right‘) { step++; //旋转到原位置,即超过最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋转角度以弧度值为参数 var degree = step * 90 * Math.PI / 180; var ctx = canvas.getContext(‘2d‘); switch (step) { case 0: canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0,width,height); break; case 1: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, 0, -height,width,height); break; case 2: canvas.width = width; canvas.height = height; ctx.rotate(degree); ctx.drawImage(img, -width, -height,width,height); break; case 3: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, -width, 0,width,height); break; } } export const upload=(file,callback)=>{//传入file文件 //对图片旋转处理 let Orientation = null; //获取照片方向角属性,用户旋转控制 EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, ‘Orientation‘); }); let oReader = new FileReader(); oReader.onload = function(e) { var image = new Image(); image.src = e.target.result; debugger; image.onload = function() { var expectWidth = this.naturalWidth; var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) { expectWidth = 800; expectHeight = expectWidth * this.naturalHeight / this.naturalWidth; } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) { expectHeight = 1200; expectWidth = expectHeight * this.naturalWidth / this.naturalHeight; } var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = expectWidth; canvas.height = expectHeight; ctx.drawImage(this, 0, 0, expectWidth, expectHeight); var base64 = null; if(Orientation != "" && Orientation != 1){ switch(Orientation){ case 6://需要顺时针(向左)90度旋转 // alert(‘需要顺时针(向左)90度旋转‘); rotateImg(this,‘left‘,canvas); break; case 8://需要逆时针(向右)90度旋转 // alert(‘需要顺时针(向右)90度旋转‘); rotateImg(this,‘right‘,canvas); break; case 3://需要180度旋转 // alert(‘需要180度旋转‘); rotateImg(this,‘right‘,canvas);//转两次 rotateImg(this,‘right‘,canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 0.8); callback(base64) }; }; oReader.readAsDataURL(file); }
使用示例
<template> <div class="hello"> <h1>exif 图片数码信息</h1> <input type="file" name="pic" ref="file" accept="image/*" @change="uploadFile"/> <div id="output"> <img :src="src" > </div> </div> </template> <script> import {upload} from "../assets/tool/upload"; export default { name: ‘exif‘, data(){ return { src:‘‘ } }, methods:{ uploadFile(e){ //获取file文件 let files=e.target.files || e.dataTransfer.files; if(files.length<=0) return; let file=files[0]; let vm=this; upload(file,function(base64){vm.src=base64}) }, }, mounted() { } } </script>