js将图片转换为base64
2021-03-01 05:26
标签:turn return nload indexof UNC lan text func blank var img = "imgurl";//imgurl 就是你的图片路径 function getBase64Image(img) { var image = new Image(); showtooltip js将图片转换为base64 标签:turn return nload indexof UNC lan text func blank 原文地址:https://www.cnblogs.com/coding8832/p/14441251.html
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 ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
var dataURL = canvas.toDataURL("image/"+ext);
return dataURL;
}
image.src = img;
image.onload = function(){
var base64 = getBase64Image(image);
console.log(base64);
}