It is possible to via canvas (https://stackoverflow.com/a/58653042/8535456):
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);
}}
);
Without modifing the property (https://stackoverflow.com/a/934925/8535456):
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.naturalWidth
canvas.height = img.aturalHeight
// 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,/, "");
}
Notes that naturalWidth
and naturalHeight
are used instead of width
and height
, since the latter results into cropped images when the image is attributed or styled with fixed sizes.
However, by printing the picture to the canvas, the quality may decrease. I am wondering if there are methods that can get the images directly from the memory.