JS图片转BASE64编码

180it 2021-09-03 PM 1640℃ 0条

JS图片转BASE64编码

<script>
function image2Base64(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, img.width, img.height);
    var dataURL = canvas.toDataURL("image/png");
    return dataURL;
}



function getImgBase64(imgsrc){
    var base64="";
    var img = new Image();
    img.src=imgsrc;
    img.onload=function(){
        base64 = image2Base64(img);
        console.log(base64);
    }
}
getImgBase64("img.png");
</script>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

JS图片转BASE64编码