js image转base64编码

发布于:2022-05-09 08:37

function getBase64Image(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 main() { 

var img = document.createElement('img');

 img.src = './images/yeoman.png'

 img.onload =function() { 

var data = getBase64Image(img); 

 console.log(data); 

 } 

 document.body.appendChild(img);

 } 

 main();