Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image data URL in JavaScript?

I have a regular HTML page with some images (just regular <img /> HTML tags). I'd like to get their content, base64 encoded preferably, without the need to redownload the image (ie. it's already loaded by the browser, so now I want the content).

I'd love to achieve that with Greasemonkey and Firefox.

like image 213
Detariael Avatar asked Jun 01 '09 08:06

Detariael


People also ask

How do I find the URL of an image?

To convert image from an Html page tag to a data URI using javascript, you first need to create a canvas element, set its width and height equal to that of the image, draw the image on it and finally call the toDataURL method on it.

What is data URL in javascript?

A Data URL is a URI scheme that provides a way to inline data in a document, and it's commonly used to embed images in HTML and CSS.

How can I get image data from image?

The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas. Note: The ImageData object is not a picture, it specifies a part (rectangle) on the canvas, and holds information of every pixel inside that rectangle.

How do I find an image URL in canvas?

To get the image data URL of the canvas, we can use the toDataURL() method of the canvas object which converts the canvas drawing into a 64 bit encoded PNG URL. If you'd like for the image data URL to be in the jpeg format, you can pass image/jpeg as the first argument in the toDataURL() method.


1 Answers

Note: This only works if the image is from the same domain as the page, or has the crossOrigin="anonymous" attribute and the server supports CORS. It's also not going to give you the original file, but a re-encoded version. If you need the result to be identical to the original, see Kaiido's answer.


You will need to create a canvas element with the correct dimensions and copy the image data with the drawImage function. Then you can use the toDataURL function to get a data: url that has the base-64 encoded image. Note that the image must be fully loaded, or you'll just get back an empty (black, transparent) image.

It would be something like this. I've never written a Greasemonkey script, so you might need to adjust the code to run in that environment.

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,/, ""); } 

Getting a JPEG-formatted image doesn't work on older versions (around 3.5) of Firefox, so if you want to support that, you'll need to check the compatibility. If the encoding is not supported, it will default to "image/png".

like image 134
Matthew Crumley Avatar answered Oct 02 '22 09:10

Matthew Crumley