如何将图片路径转化成base64格式

function image(imgurl ) {   //imgurl   本地图片路径  
	var img = imgurl ;
	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 ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
		var dataURL = canvas.toDataURL("image/" + ext);
		return dataURL;
	}

	var image = new Image();
	image.src = img;
	image.onload = function() {
		var base64 = getBase64Image(image);    //转换之后的图片路径
	}
}

你可能感兴趣的:(如何将图片路径转化成base64格式)