Base64 압축

1482 단어




    
    
     




     



$("#file").change(function(){
var m_this = this;
      cutImageBase64(m_this,null,400,0.8);  
})
function cutImageBase64(m_this,id,wid,quality) {

    var file = m_this.files[0];
    var URL = window.URL || window.webkitURL;
    var blob = URL.createObjectURL(file);
    var base64;
    var quality=0.8;
var img = new Image();
    img.src = blob;
    img.onload = function() {
        var that = this;
        // 
        var w = that.width,
            h = that.height,
            scale = w / h;
            w = wid || w;
            h = w / scale;
// canvas
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        $(canvas).attr({
            width: w,
            height: h
        });
        ctx.drawImage(that, 0, 0, w, h);
    var size=file.size/1024;
    //  , 50-150kb , ,quality 
				 while (size > 64) {
				 	quality -= 0.1;
				 	size=canvas.toDataURL('image/jpeg', quality);
				 }
				//  , quality , 
				 while (size < 12) {
				 	quality += 0.1;
				 	size=canvas.toDataURL('image/jpeg', quality);
				 }

        //  base64            
        base64 = canvas.toDataURL('image/jpeg', quality);
        console.log(base64)
        console.log(base64.length/1024)
        console.log(file.size/1024)
    };
}



좋은 웹페이지 즐겨찾기