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

将本地图片获取到,转成base64数据

武琛
2023-12-01

ToBase64() {
function getBase64Image(img) {
var canvas = document.createElement(“canvas”);
canvas.width = 200;
canvas.height = 200;
var ctx = canvas.getContext(“2d”);
ctx.drawImage(img, 0, 0, img.width, img.height);
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
var dataURL = canvas.toDataURL(“image/”+ext);
return dataURL;
}
var img = “…/…/static/images/img-girl.png”;
var image = new Image();
image.src = img;
image.onload = function(){
this.base64Img = getBase64Image(image);
console.log(this.base64Img);
}

},
 类似资料: