想要找点什么呢?

幕冬有柒

将图片压缩转成base64

幕冬有柒 · 2021-04-20 · 245 次阅读

//将图片压缩转成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);
    }
}

  • 微信

Comments | 0 条评论