js图片转base64
/** * 将图片转换为Base64 */
function image2Base64(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;
}
/** * 获取图片Base64编码 */
function getImgBase64()
{
var base64=""; var img = new Image();
img.src="img/test.jpg";
img.onload=function(){
base64 = image2Base64(img);
}
}
getImgBase64();