用JavaScript获取图像数据url?我有一个带有一些图像的常规HTML页面(只是普通的)<img />HTML标记)。我想得到他们的内容,base 64最好编码,不需要重新下载图像(即。它已经被浏览器加载,所以现在我想要内容)。我很想用Greemonkey和Firefox来实现这一点。
3 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
注:crossOrigin="anonymous"
drawImage
toDataURL
function getBase64Image(img) { // Create an empty canvas element var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; // Copy the image contents to the canvas var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); // Get the data-URL formatted image // Firefox supports PNG and JPEG. You could check img.src to // guess the original format, but be aware the using "image/jpg" // will re-encode the image. var dataURL = canvas.toDataURL("image/png"); return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");}
MMMHUHU
TA贡献1834条经验 获得超8个赞
function getBase64FromImageUrl(url) { var img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.onload = function () { var canvas = document.createElement("canvas"); canvas.width =this.width; canvas.height =this.height; var ctx = canvas.getContext("2d"); ctx.drawImage(this, 0, 0); var dataURL = canvas.toDataURL("image/png"); alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, "")); }; img.src = url;}
getBase64FromImageUrl("images/slbltxt.png")
添加回答
举报
0/150
提交
取消