欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

移動(dòng)端圖片上傳旋轉(zhuǎn)、壓縮問(wèn)題的方法

 更新時(shí)間:2018年10月16日 08:52:02   投稿:laozhang  
在本篇文章中我們給大家分享了關(guān)于移動(dòng)端圖片上傳旋轉(zhuǎn)、壓縮問(wèn)題的解決方法,有需要的朋友們參考下

前言

在手機(jī)上通過(guò)網(wǎng)頁(yè) input 標(biāo)簽拍照上傳圖片,有一些手機(jī)會(huì)出現(xiàn)圖片旋轉(zhuǎn)了90度d的問(wèn)題,包括 iPhone 和個(gè)別三星手機(jī)。這些手機(jī)豎著拍的時(shí)候才會(huì)出現(xiàn)這種問(wèn)題,橫拍出來(lái)的照片就正常顯示。因此,可以通過(guò)獲取手機(jī)拍照角度來(lái)對(duì)照片進(jìn)行旋轉(zhuǎn),從而解決這個(gè)問(wèn)題。

Orientation

這個(gè)參數(shù)并不是所有圖片都有的,不過(guò)手機(jī)拍出來(lái)的圖片是帶有這個(gè)參數(shù)的。

旋轉(zhuǎn)角度 參數(shù)值
1
順時(shí)針90° 6
逆時(shí)針90° 8
180° 3

參數(shù)為 1 的時(shí)候顯示正常,那么在這些橫拍顯示正常,即 Orientation = 1 的手機(jī)上,豎拍的參數(shù)為 6。

想要獲取 Orientation 參數(shù),可以通過(guò) exif.js 庫(kù)來(lái)操作。exif.js 功能很多,體積也很大,未壓縮之前足足有 30k,這對(duì)手機(jī)頁(yè)面加載來(lái)說(shuō)是非常大影響的。而我只需要獲取 Orientation 信息而已,所以我這里刪減了 exif.js 庫(kù)的一些代碼,將代碼縮小到幾KB。

exif.js 獲取 Orientation :

EXIF.getData(file, function() { 
 var Orientation = EXIF.getTag(this, 'Orientation');
});

file 則是 input 文件表單上傳的文件。上傳的文件經(jīng)過(guò) fileReader.readAsDataURL(file) 就可以實(shí)現(xiàn)預(yù)覽圖片了,這方面不清楚的可以查看:HTML5 進(jìn)階系列:文件上傳下載

旋轉(zhuǎn)

旋轉(zhuǎn)需要用到 canvas 的 rotate() 方法。

ctx.rotate(angle);

rotate 方法的參數(shù)為旋轉(zhuǎn)弧度。需要將角度轉(zhuǎn)為弧度:degrees * Math.PI / 180

旋轉(zhuǎn)的中心點(diǎn)默認(rèn)都在 canvas 的起點(diǎn),即 ( 0, 0 )。旋轉(zhuǎn)的原理如下圖:

旋轉(zhuǎn)之后,如果從 ( 0, 0 ) 點(diǎn)進(jìn)行 drawImage(),那么畫(huà)出來(lái)的位置就是在左圖中的旋轉(zhuǎn) 90 度后的位置,不在可視區(qū)域呢。旋轉(zhuǎn)之后,坐標(biāo)軸也跟著旋轉(zhuǎn)了,想要顯示在可視區(qū)域呢,需要將 ( 0, 0 ) 點(diǎn)往 y 軸的反方向移 y 個(gè)單位,此時(shí)的起始點(diǎn)則為 ( 0, -y )。

同理,可以獲得旋轉(zhuǎn) -90 度后的起始點(diǎn)為 ( -x, 0 ),旋轉(zhuǎn) 180 度后的起始點(diǎn)為 ( -x, -y )。

壓縮

手機(jī)拍出來(lái)的照片太大,而且使用 base64 編碼的照片會(huì)比原照片大,那么上傳的時(shí)候進(jìn)行壓縮就非常有必要的。現(xiàn)在的手機(jī)像素這么高,拍出來(lái)的照片寬高都有幾千像素,用 canvas 來(lái)渲染這照片的速度會(huì)相對(duì)比較慢。

因此第一步需要先對(duì)上傳照片的寬高做限制,判斷寬度或高度是否超出哪個(gè)范圍,則等比壓縮其寬高。

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;
}

第二步就通過(guò) canvas.toDataURL() 方法來(lái)壓縮照片質(zhì)量。

canvas.toDataURL("image/jpeg", 1);

toDataURL() 方法返回一個(gè)包含圖片展示的 data URI 。使用兩個(gè)參數(shù),第一個(gè)參數(shù)為圖片格式,默認(rèn)為 image/png。第二個(gè)參數(shù)為壓縮質(zhì)量,在指定圖片格式為 image/jpeg 或 image/webp的情況下,可以從 0 到 1 的區(qū)間內(nèi)選擇圖片的質(zhì)量。

總結(jié)

綜合以上,例子的代碼包括精簡(jiǎn)的exif.js庫(kù)地址: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: // 旋轉(zhuǎn)90度
      canvas.width = imgHeight; 
      canvas.height = imgWidth; 
      ctx.rotate(Math.PI / 2);       
      // (0,-imgHeight) 從旋轉(zhuǎn)原理圖那里獲得的起始點(diǎn)
      ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight); 
      break;      
      case 3: // 旋轉(zhuǎn)180度
      ctx.rotate(Math.PI); 
      ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight); 
      break;      
      case 8: // 旋轉(zhuǎn)-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>

相關(guān)文章

最新評(píng)論