前言
在手機上透過網頁 input 標籤拍照上傳圖片,有些手機會出現圖片旋轉了90度d的問題,包括 iPhone 和個別三星手機。這些手機豎起拍的時候才會出現這種問題,橫拍出來的照片就正常顯示。因此,可以透過獲取手機拍照角度來對照片進行旋轉,從而解決這個問題。
Orientation
這個參數並不是所有圖片都有的,不過手機拍出來的圖片是帶有這個參數的。
0° |
#1 |
順時針90° |
6 |
#逆時針90° |
8 |
180° |
3 |
#參數為1 的時候顯示正常,那麼在這些橫拍顯示正常,即Orientation = 1 的手機上,垂直拍攝的參數為6。
想要取得 Orientation 參數,可以透過 exif.js 函式庫來操作。 exif.js 功能很多,體積也很大,未壓縮之前足足有 30k,這對手機頁面載入來說是非常大影響的。而我只需要取得 Orientation 資訊而已,所以我這裡刪減了 exif.js 函式庫的一些程式碼,將程式碼縮小到幾KB。
exif.js 取得 Orientation :
1 | EXIF.getData(file, function () { var Orientation = EXIF.getTag(this, 'Orientation');});
|
登入後複製
file 則是 input 檔案表單上傳的檔案。上傳的檔案經過fileReader.readAsDataURL(file) 就可以實現預覽圖片了,這方面不清楚的可以檢視:HTML5 進階系列:檔案上傳下載
旋轉
#旋轉需要用到canvas 的rotate() 方法。
rotate 方法的參數為旋轉弧度。需要將角度轉為弧度:degrees * Math.PI / 180
旋轉的中心點預設都在 canvas 的起點,即 ( 0, 0 )。旋轉的原理如下圖:

旋轉之後,如果從( 0, 0 ) 點進行drawImage(),那麼畫出來的位置就是在左圖中的旋轉90度後的位置,不在可視區域。旋轉之後,座標軸也跟著旋轉了,想要顯示在可視區域呢,需要將( 0, 0 ) 點往y 軸的反方向移y 個單位,此時的起始點則為( 0, -y )。
同理,可以得到旋轉 -90 度後的起始點為 ( -x, 0 ),旋轉 180 度後的起始點為 ( -x, -y )。
壓縮
手機拍出來的照片太大,而且使用 base64 編碼的照片會比原始照片大,那麼上傳的時候進行壓縮就非常有必要的。現在的手機像素這麼高,拍出來的照片寬高都有數千像素,用 canvas 來渲染這張照片的速度會相對比較慢。
因此第一步需要先對上傳照片的寬高做限制,判斷寬度或高度是否超出哪個範圍,則等比壓縮其寬高。
1 | 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() 方法來壓縮照片品質。
1 | canvas.toDataURL( "image/jpeg" , 1);
|
登入後複製
toDataURL() 方法傳回一個包含圖片展示的 data URI 。使用兩個參數,第一個參數為圖片格式,預設為 image/png。第二個參數為壓縮質量,在指定圖片格式為 image/jpeg 或 image/webp的情況下,可以從 0 到 1 的區間內選擇圖片的品質。
總結
綜合以上,範例的程式碼包括精簡的exif.js庫位址:file-demo
主要的核心程式碼如下:
##
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <input type= "file" id= "files" ><img src= "/static/imghw/default1.png" data-src= "blank.gif" class = "lazy" id= "preview" alt= "行動端圖片上傳旋轉、壓縮的解決方案" >
<script src= "small-exif.js?1.1.11" ></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;
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:
ctx.rotate(Math.PI / 2);
break ; case 3:
ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight); break ; case 8:
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>
|
登入後複製