利用html5壓縮圖片,產出位元據流

來源:互聯網
上載者:User

標籤:

 

/*  將頁面選擇的圖片等比壓縮成指定大小(長邊固定)
  file:圖片檔案
callBack:回呼函數
maxLen:長邊的長度
*/
function makePic(file,callBack,maxLen){
    var url = webkitURL.createObjectURL(file);  /* 產生圖片 */  var $img = new Image();  $img.src = url;  // $(‘body‘).append($img);  $img.onload = function() {    //產生比例    var width = $img.width,height = $img.height;    //計算縮放比例    var rate=1;    if(width>=height){      if(width>maxLen){        rate=maxLen/width;      }    }else{      if(height>maxLen){        rate=maxLen/height;      }   }  $img.width=width*rate;  $img.height=height*rate;  //產生canvas  var $canvas =$(‘#thecanvas‘);  var ctx = $canvas[0].getContext(‘2d‘);  $canvas.attr({width : $img.width, height : $img.height});  ctx.drawImage($img, 0, 0, $img.width, $img.height);  var base64 = $canvas[0].toDataURL(‘image/jpeg‘,0.9);  callBack(base64);//  }}$(‘.unPic_list input[type=file]‘).bind(‘change‘,function(e){  var that=this;  for(var j=0;j<e.target.files.length;j++){    makePic(e.target.files[j],function(imgStr){      var img = new Image();      img.src=imgStr;      img.onload=function(){      $(‘body‘).append(img);
      /*此處將 imgStr.substr(22)操作即的圖片二進位流,可發送到伺服器*/    }   },750); } })

利用html5壓縮圖片,產出位元據流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.