图片转base64
html:
<img src="img1.png">
js:
1 //海报转base64 2 function getBase64Image(img) { 3 var canvas = document.createElement("canvas"); 4 canvas.width = img.width; 5 canvas.height = img.height; 6 var ctx = canvas.getContext("2d"); 7 ctx.drawImage(img, 0, 0, img.width, img.height); 8 var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase(); 9 var dataURL = canvas.toDataURL("image/" + ext); 10 return dataURL; 11 } 12 13 var img = $("img").prop("src"); 14 var image = new Image(); 15 image.src = img; 16 var small_base64 = getBase64Image(image);