原文地址:http://blog.gdfengshuo.com/2017/06/29/17html
在手機上經過網頁 input 標籤拍照上傳圖片,有一些手機會出現圖片旋轉了90度d的問題,包括 iPhone 和個別三星手機。這些手機豎着拍的時候纔會出現這種問題,橫拍出來的照片就正常顯示。所以,能夠經過獲取手機拍照角度來對照片進行旋轉,從而解決這個問題。git
這個參數並非全部圖片都有的,不過手機拍出來的圖片是帶有這個參數的。github
旋轉角度 | 參數值 |
---|---|
0° | 1 |
順時針90° | 6 |
逆時針90° | 8 |
180° | 3 |
參數爲 1 的時候顯示正常,那麼在這些橫拍顯示正常,即 Orientation = 1 的手機上,豎拍的參數爲 6。web
想要獲取 Orientation 參數,能夠經過 exif.js 庫來操做。exif.js 功能不少,體積也很大,未壓縮以前足足有 30k,這對手機頁面加載來講是很是大影響的。而我只須要獲取 Orientation 信息而已,因此我這裏刪減了 exif.js 庫的一些代碼,將代碼縮小到幾KB。canvas
exif.js 獲取 Orientation :this
EXIF.getData(file, function() { var Orientation = EXIF.getTag(this, 'Orientation'); });
file 則是 input 文件表單上傳的文件。上傳的文件通過 fileReader.readAsDataURL(file) 就能夠實現預覽圖片了,這方面不清楚的能夠查看:HTML5 進階系列:文件上傳下載編碼
旋轉須要用到 canvas 的 rotate() 方法。code
ctx.rotate(angle);
rotate 方法的參數爲旋轉弧度。須要將角度轉爲弧度:degrees * Math.PI / 180htm
旋轉的中心點默認都在 canvas 的起點,即 ( 0, 0 )。旋轉的原理以下圖:blog
旋轉以後,若是從 ( 0, 0 ) 點進行 drawImage(),那麼畫出來的位置就是在左圖中的旋轉 90 度後的位置,不在可視區域呢。旋轉以後,座標軸也跟着旋轉了,想要顯示在可視區域呢,須要將 ( 0, 0 ) 點往 y 軸的反方向移 y 個單位,此時的起始點則爲 ( 0, -y )。
同理,能夠得到旋轉 -90 度後的起始點爲 ( -x, 0 ),旋轉 180 度後的起始點爲 ( -x, -y )。
手機拍出來的照片太大,並且使用 base64 編碼的照片會比原照片大,那麼上傳的時候進行壓縮就很是有必要的。如今的手機像素這麼高,拍出來的照片寬高都有幾千像素,用 canvas 來渲染這照片的速度會相對比較慢。
所以第一步須要先對上傳照片的寬高作限制,判斷寬度或高度是否超出哪一個範圍,則等比壓縮其寬高。
var ratio = width / height; if(imgWidth > imgHeight && imgWidth > xx){ imgWidth = xx; imgHeight = Math.ceil(xx / ratio); }else if(imgWidth < imgHeight && imgHeight > yy){ imgWidth = Math.ceil(yy * ratio); imgHeight = yy; }
第二步就經過 canvas.toDataURL() 方法來壓縮照片質量。
canvas.toDataURL("image/jpeg", 1);
toDataURL() 方法返回一個包含圖片展現的 data URI 。使用兩個參數,第一個參數爲圖片格式,默認爲 image/png。第二個參數爲壓縮質量,在指定圖片格式爲 image/jpeg 或 image/webp的狀況下,能夠從 0 到 1 的區間內選擇圖片的質量。
綜合以上,例子的代碼包括精簡的exif.js庫地址:file-demo
主要的核心代碼以下:
<input type="file" id="files" > <img src="blank.gif" id="preview"> <script src="small-exif.js"></script> <script> var ipt = document.getElementById('files'), img = document.getElementById('preview'), Orientation = null; ipt.onchange = function () { var file = ipt.files[0], reader = new FileReader(), image = new Image(); if(file){ EXIF.getData(file, function() { Orientation = EXIF.getTag(this, 'Orientation'); }); reader.onload = function (ev) { image.src = ev.target.result; image.onload = function () { var imgWidth = this.width, imgHeight = this.height; // 控制上傳圖片的寬高 if(imgWidth > imgHeight && imgWidth > 750){ imgWidth = 750; imgHeight = Math.ceil(750 * this.height / this.width); }else if(imgWidth < imgHeight && imgHeight > 1334){ imgWidth = Math.ceil(1334 * this.width / this.height); imgHeight = 1334; } var canvas = document.createElement("canvas"), ctx = canvas.getContext('2d'); canvas.width = imgWidth; canvas.height = imgHeight; if(Orientation && Orientation != 1){ switch(Orientation){ case 6: // 旋轉90度 canvas.width = imgHeight; canvas.height = imgWidth; ctx.rotate(Math.PI / 2); // (0,-imgHeight) 從旋轉原理圖那裏得到的起始點 ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight); break; case 3: // 旋轉180度 ctx.rotate(Math.PI); ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight); break; case 8: // 旋轉-90度 canvas.width = imgHeight; canvas.height = imgWidth; ctx.rotate(3 * Math.PI / 2); ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight); break; } }else{ ctx.drawImage(this, 0, 0, imgWidth, imgHeight); } img.src = canvas.toDataURL("image/jpeg", 0.8); } } reader.readAsDataURL(file); } } </script>