HTML

<img id="imageid" src="https://www.google.de/images/srpr/logo11w.png">

JavaScript

function getBase64Image(img) {
  var canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);
  var dataURL = canvas.toDataURL("image/png");
  return dataURL.replace(/^data:image\/?[A-z]*;base64,/);
}

var base64 = getBase64Image(document.getElementById("imageid"));

Special thanks to @Md. Hasan Mahmud for providing an improved regex that works with any image mime type in my comments!

This method requires the canvas element, which is perfectly supported.

  • The MDN reference of HTMLCanvasElement.toDataURL().
  • And the official W3C documentation.
Answer from ˈvɔlə on Stack Overflow
🌐
Tick
pqina.nl › blog › convert-an-image-to-a-base64-string-with-javascript
Convert An Image To A DataURL or Base64 String Using JavaScript - Pqina
We’ll be converting images to DataURLs, we can use the function below to convert a DataURL to a Base64 string. const getBase64StringFromDataURL = (dataURL) => dataURL.replace('data:', '').replace(/^.+,/, ''); Let’s get started.
🌐
Medium
medium.com › @j622amilah › javascript-basics-45-154fcf627ab5
JavaScript Basics 45. Image url fetch, base64 encoding for… | by Practicing DatScy | Medium
May 19, 2026 - // document.getElementById(this_id).src = "data:image/jpg;base64,"+binaryString_base64; // OR // [way 1] reconstruct the base64 to a blob, the load a blob. var binaryString = atob(binaryString_base64); document.getElementById("p0").innerHTML ...
Discussions

convert base64 to image in javascript/jquery - Stack Overflow
I think I'm looking for the same thing OP is. I believe he wants the image url to point to a .png, not the original base64 encoded string. Looking for a conversion somewhere in there if it is possible. More on stackoverflow.com
🌐 stackoverflow.com
Is there a way to convert the s3 image URL to base64.?

You don’t want to convert the URL to base64, how would that help? You want to convert the image content to base64.

More on reddit.com
🌐 r/learnjavascript
3
1
August 23, 2022
Is there a way to convert the s3 image URL to base64 in nodejs
If the S3 object is publicly available, node-fetch can get the buffer, which you can then convert to base64, had to do this myself today. More on reddit.com
🌐 r/node
9
3
August 23, 2022
Need to display huge PDFs from base64 string, but browser has limitations.
Try this https://stackoverflow.com/a/71075493 More on reddit.com
🌐 r/webdev
14
5
November 22, 2022
🌐
TutorialsPoint
tutorialspoint.com › converting-images-to-a-base64-data-url-using-javascript
Convert Image to Data URI with JavaScript
August 26, 2022 - <!DOCTYPE html> <html> <head> <title>Convert Image URL to Data URI</title> </head> <body> <img src="https://via.placeholder.com/150" id="myImg" alt="Sample Image"> <button onclick="convertImage()">Convert to Data URI</button> <div id="output"></div> ...
🌐
DEV Community
dev.to › snappy_tools › image-to-base64-the-complete-guide-with-html-css-and-javascript-examples-k9h
Image to Base64: The Complete Guide (with HTML, CSS, and JavaScript examples) - DEV Community
May 18, 2026 - ... The browser renders this exactly like a normal <img> tag. No network request needed. ... .icon { background-image: url('data:image/png;base64,iVBORw0KGgo...'); width: 24px; height: 24px; background-size: contain; }
🌐
Staxmanade
staxmanade.com › 2017 › 02 › how-to-download-and-convert-an-image-to-base64-data-url
How to Download and Convert an Image to Base64 Data Url - Developing on Staxmanade
February 26, 2017 - getBase64ImageFromUrl('http://approvaltests.com/images/logo.png') .then(result => testImage.src = result) .catch(err => console.error(err)); As an alternative to base64 encoded FileReader approach above, you can also also use the URL.createObjectURL return URL.createObjectURL(blob);
🌐
The Web Dev
thewebdev.info › home › how to get image data as a base64 url in javascript?
How to Get Image Data as a Base64 URL in JavaScript? - The Web Dev
March 20, 2021 - We can convert an image to a base64 URL by loading the image with the given URL into the canvas and then call toDataURL to convert it to base64. How to Parse a URL into the Hostname and Path in JavaScript?
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › convert-image-to-base64-using-node
Convert an Image or an Image URL to base64 in Node.js | bobbyhadz
The imageUrlToBase64() function takes the complete URL that points to the image as a parameter and converts the image URL to a base64 string.
🌐
CodePen
codepen.io › jamiekane › pen › YayWOa
Base64 Image Example
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71d
🌐
YouTube
youtube.com › watch
How to Convert Images Into Base 64 Data URLs with JavaScript - YouTube
In today's tutorial I'll show you how to convert an image to data URL in JavaScript. This is very easy to do by using the FileReader object.For your referenc...
Published: August 16, 2022
🌐
DEV Community
dev.to › dcodeyt › how-to-convert-images-to-base64-data-urls-in-javascript-2n3i
How to Convert Images to Base64 Data URLs in JavaScript - DEV Community
August 31, 2022 - const fileInput = document.getElementById("fileInput"); fileInput.addEventListener("change", e => { const file = fileInput.files[0]; const reader = new FileReader(); reader.addEventListener("load", () => { // Base64 Data URL 👇 console.lo...
🌐
W3docs
w3docs.com › javascript
How to Convert the Image into a Base64 String Using JavaScript | W3Docs
There are several approaches in JavaScript that can help you with converting the image into a Base64 string. Firstly, create a canvas, then load the image into it and use toDataURL() to get the Base64 representation. In fact, it is a data URL, but it contains the Base64-encoded image:
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
Image to Base64 | Base64 Encode | Base64 Converter | Base64
Convert image to Base64 online and use the result string as data URI, img src, CSS background-url, and others. Sometimes you have to send or output an image within a text document (for example, HTML, CSS, JSON, XML), but you cannot do this because binary characters will damage the syntax of the text document.
🌐
codestudy
codestudy.net › blog › convert-image-from-url-to-base64
How to Convert Image URL to Base64 in JavaScript: Step-by-Step Guide for Local Saving — codestudy.net
HTML5 Understanding: Awareness of the <canvas> element and Blob/File APIs. Tools: A code editor (e.g., VS Code), a modern browser (Chrome, Firefox, Edge), and an image URL for testing (e.g., https://example.com/image.jpg).
🌐
Tick
pqina.nl › blog › convert-a-file-to-a-base64-string-with-javascript
Convert A File To A Base64 String Or DataURL Using JavaScript - Pqina
<input type="file" /> <script> // Get a reference to the file input const fileInput = document.querySelector('input'); // Listen for the change event so we can capture the file fileInput.addEventListener('change', (e) => { // Get a reference to the file const file = e.target.files[0]; // Encode the file using the FileReader API const reader = new FileReader(); reader.onloadend = () => { // Use a regex to remove data url part const base64String = reader.result .replace('data:', '') .replace(/^.+,/, ''); console.log(base64String); // Logs wL2dvYWwgbW9yZ... }; reader.readAsDataURL(file); }); </script> Sometimes we just want to use an File object as an image source.
🌐
CodePen
codepen.io › caeroa › pen › MWeyGYz
Image to Base64 url
Minimize JavaScript Editor · Fold All · Unfold All · /** * Convert an image to a base64 url * @param {String} url * @param {String} [outputFormat=image/png] */ function convertImageToBase64(img, outputFormat) { var originalWidth = ...
🌐
Formspree
formspree.io › blog › image-to-base64
How to Convert an Image URL to Base64 | Formspree
August 28, 2024 - Fetching the Image: The Fetch API ... image URL, retrieving the image data as a response object. Converting to ArrayBuffer: The response object typically contains the image data in a binary format. You can use the arrayBuffer() method to convert this binary data into an ArrayBuffer, a more efficient representation for manipulation. Base64 Encoding with btoa: Once you have the image data as an ArrayBuffer, you can leverage the built-in btoa function in JavaScript...