当前位置: 首页 > 工具软件 > getbase > 使用案例 >

JS获取图片base64

丁振海
2023-12-01
function onSendImage() {

var img = document.createElement('img');
img.id = "img";
img.src = pathPhoto;
img.style.width = "100%";
img.style.height = "100%";
img.onload = function() {


getBase64Image(img);
}

    document.body.appendChild(img);
}


function getBase64Image(img) {
var base64, base;
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);
base = canvas.toDataURL("image/png");
base64 = base.toString().substr(base.toString().indexOf(",") + 1);

}

 类似资料: