将图片压缩转成base64

发布于 2020-11-11  68 次阅读


//将图片压缩转成base64
function upimg(_this){
     var current_pic=_this.files[0];
     var URL = window.URL || window.webkitURL;
    var blob = URL.createObjectURL(current_pic);
     var image = new Image();
    image.src = blob;
    image.onload=function(e){
        var that = this;
        var w = that.width,
        h = that.height,
        scale = w / h;
        w = 800 || w;
        h = w / scale;
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        (canvas).attr({width:w,height:h});
        ctx.drawImage(that, 0, 0, w, h);
        // 生成base64                   var base64 = canvas.toDataURL('image/jpeg',0.8);('#img').attr('src',base64);
    }
}

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。