JS将图片链接转为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格式:

var base64="";
var img = new Image();
img.src="img/test.jpg";
img.onload=function(){
    base64 = image2Base64(img);
    alert(base64);
}