//這是一個解決exif更改脫方向的一個js文件 // 應用前先npm install exif-js --save或者直接引入exif-js //// 調用方法 // let baseData; // imgExif(file,function (base){ // baseData=base // }); import Exif from 'exif-js' //引入exif let u = {}; // 轉換的主體方法 u.index = (file, callback) => { let Orientation, resultData; //圖片的方向,返回的值 //去獲取拍照時的信息,解決拍出來的照片旋轉問題 Exif.getData(file, function () { Orientation = Exif.getTag(this, 'Orientation'); }); // 看支持不支持FileReader if (!file || !window.FileReader) return; // 看支持不支持FileReader if (!file || !window.FileReader) return; if (/^image/.test(file.type)) { // 創建一個reader let reader = new FileReader(); // 將圖片2將轉成 base64 格式 reader.readAsDataURL(file); // 讀取成功后的回調 reader.onloadend = function () { let result = this.result; //判斷圖片是否有方向值沒有直接返回base64 if (!Orientation) { callback(result); } else { let img = new Image(); img.src = result; //圖片信息加載完轉換方向 img.onload = function () { resultData = u.compress(img, Orientation); callback(resultData); } } } } }; //更改旋轉方向 u.compress = (img, Orientation) => { let canvas = document.createElement("canvas"), ctx = canvas.getContext('2d'), tCanvas = document.createElement("canvas"), tctx = tCanvas.getContext("2d"), initSize = img.src.length, width = img.width, height = img.height, ratio = 1; //聲明 canvas.width = width; canvas.height = height; // 鋪底色 ctx.fillStyle = "#fff"; ctx.fillRect(0, 0, canvas.width, canvas.height); //如果圖片像素大於100萬則使用瓦片繪制 let count; if ((count = width * height / 1000000) > 1) { // console.log("超過100W像素"); count = ~~(Math.sqrt(count) + 1); //計算要分成多少塊瓦片 // 計算每塊瓦片的寬和高 let nw = ~~(width / count); let nh = ~~(height / count); tCanvas.width = nw; tCanvas.height = nh; for (let i = 0; i < count; i++) { for (let j = 0; j < count; j++) { tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh); ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh); } } } else { ctx.drawImage(img, 0, 0, width, height); } //修復上傳圖片的時候被旋轉的問題 if (Orientation != "" && Orientation != 1) { switch (Orientation) { case 6://需要順時針(向左)90度旋轉 u.rotateImg(img, 'left', canvas); break; case 8://需要逆時針(向右)90度旋轉 u.rotateImg(img, 'right', canvas); break; case 3://需要180度旋轉 u.rotateImg(img, 'right', canvas, 2);//轉兩次 break; } } //進行最小壓縮 let ndata = canvas.toDataURL('image/jpeg', 1); tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0; return ndata; }; //更改旋轉方向 u.rotateImg = (img, direction, canvas, num) => { //最小與最大旋轉方向,圖片旋轉4次后回到原方向 const min_step = 0; const max_step = 3; if (img == null) return; //img的高度和寬度不能在img元素隱藏后獲取,否則會出錯 let height = img.height; let width = img.width; let 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); } //旋轉180度 step = num ? num : step; //旋轉角度以弧度值為參數 let degree = step * 90 * Math.PI / 180; let ctx = canvas.getContext('2d'); switch (step) { case 0: canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0); break; case 1: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, 0, -height); break; case 2: canvas.width = width; canvas.height = height; ctx.rotate(degree); ctx.drawImage(img, -width, -height); break; case 3: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, -width, 0); break; } }; window.imgExif = u;
就是那么殘暴直接上代碼
要是后台需要的是file用這個轉換一下就可以了
//將base64轉換為img文件 u.compress = (img, Orientation) => { let 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}); };
為啥少於150字的隨筆不能發布到博客我的天呀!!直接代碼不是好點嗎???我這個說不了那么多話呀,,這好折磨人呀,,