Use canvas..Here is the code copied from a program
function fun_nam(url,back, op){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(op);
back(dataURL);
canvas = null;
};
img.src = url;
}
Some details ...
The HTMLCanvasElement.toDataURL() method returns a data URI containing a representation of the image in the format specified by the type parameter (defaults to PNG). The returned image is in a resolution of 96 dpi.
Syntax: canvas.toDataURL(type, encoderOptions);
Parameters
type | Optional
- A DOMString indicating the image format. The default type is image/png.
encoderOptions | Optional
- A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored.
Return value
- A DOMString containing the requested data URI.
Actually we are drawing the image data with the drawImage function,Then use the toDataURL
function to get a base-64 encoded image data: url.
The encoding should be do only after loading the full image.Else you will get some broken/grey type image after decoding.