js壓縮圖片


1.js

/*canvas壓縮圖片
   *width:壓縮后寬度
   *height:壓縮后高度
   *img:img dom對象
   */
    function compressImg(width, height, img) {
        var canvas = document.createElement('canvas'), c = canvas.getContext('2d');
        canvas.width = width;
        canvas.height = height;
        c.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, width, height);
        return canvas.toDataURL('image/png');
    }

2.按比例壓縮調用方式

var img = new Image();
img.src = reader.result;
img.onload = function () {
var w = this.width,
h = this.height,
scale = w / h;
//壓縮圖片
var b64 = compressImg(600, 600 / scale, img);
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM