我有一个带有一些图像的常规HTML页面(只是常规的<img /> HTML标签)。我想得到他们的内容,base64编码最好,而不需要重新下载图像(即。它已经被浏览器加载,所以现在我想要的内容)。
我希望通过Greasemonkey和Firefox实现这一目标。
我有一个带有一些图像的常规HTML页面(只是常规的<img /> HTML标签)。我想得到他们的内容,base64编码最好,而不需要重新下载图像(即。它已经被浏览器加载,所以现在我想要的内容)。
我希望通过Greasemonkey和Firefox实现这一目标。
当前回答
Shiv / shim / sham
如果你的图像已经加载(或没有加载),这个“工具”可能会派上用场:
Object.defineProperty
(
HTMLImageElement.prototype,'toDataURL',
{enumerable:false,configurable:false,writable:false,value:function(m,q)
{
let c=document.createElement('canvas');
c.width=this.naturalWidth; c.height=this.naturalHeight;
c.getContext('2d').drawImage(this,0,0); return c.toDataURL(m,q);
}}
);
. .但是为什么呢?
这样做的好处是使用“已经加载”的图像数据,因此不需要额外的请求。此外,它还允许最终用户(像您这样的程序员)决定CORS和/或mime类型和质量- or -您可以省略MDN规范中描述的这些参数/参数。
如果你已经加载了这个JS(在需要它之前),那么转换为dataURL就像这样简单:
例子
HTML
<img src="/yo.jpg" onload="console.log(this.toDataURL('image/jpeg'))">
JS
console.log(document.getElementById("someImgID").toDataURL());
GPU指纹
如果您担心比特的“精确性”,那么您可以根据@Kaiido的回答修改这个工具以满足您的需求。
其他回答
这个函数接受URL,然后返回BASE64图像
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(“图像/ slbltxt.png”)
kaiido使用fetch的更现代版本的答案是:
function toObjectUrl(url) {
return fetch(url)
.then((response)=> {
return response.blob();
})
.then(blob=> {
return URL.createObjectURL(blob);
});
}
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
编辑:正如评论中指出的那样,这将返回一个指向本地系统中的文件的对象url,而不是实际的DataURL,因此根据您的用例,这可能不是您所需要的。
您可以查看下面的答案来使用fetch和一个实际的dataURL: https://stackoverflow.com/a/50463054/599602
使用onload事件转换图像加载后
函数加载(img) { 让c = document.createElement('canvas') c.getContext (2 d)。drawImage(img, 0,0) 味精。innerText = c.toDataURL实现(); } Pre {word-wrap: break-word;宽度:500 px;空白:pre-wrap;} <img onload="loaded(this)" src="https://cors-anywhere.herokuapp.com/http://lorempixel.com/200/140" crossorigin="anonymous"/> < pre id =“味精”> < / >之前
Shiv / shim / sham
如果你的图像已经加载(或没有加载),这个“工具”可能会派上用场:
Object.defineProperty
(
HTMLImageElement.prototype,'toDataURL',
{enumerable:false,configurable:false,writable:false,value:function(m,q)
{
let c=document.createElement('canvas');
c.width=this.naturalWidth; c.height=this.naturalHeight;
c.getContext('2d').drawImage(this,0,0); return c.toDataURL(m,q);
}}
);
. .但是为什么呢?
这样做的好处是使用“已经加载”的图像数据,因此不需要额外的请求。此外,它还允许最终用户(像您这样的程序员)决定CORS和/或mime类型和质量- or -您可以省略MDN规范中描述的这些参数/参数。
如果你已经加载了这个JS(在需要它之前),那么转换为dataURL就像这样简单:
例子
HTML
<img src="/yo.jpg" onload="console.log(this.toDataURL('image/jpeg'))">
JS
console.log(document.getElementById("someImgID").toDataURL());
GPU指纹
如果您担心比特的“精确性”,那么您可以根据@Kaiido的回答修改这个工具以满足您的需求。
在HTML5中最好使用这个:
{
//...
canvas.width = img.naturalWidth; //img.width;
canvas.height = img.naturalHeight; //img.height;
//...
}