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
🌐
Elmah.io
elmah.io › tools › base64-image-encoder
Base64 Image Encoder - Convert any image file or URL online
With elmah.io's free image to Base64 encoder, it's easy to copy and paste markup or style for exactly your codebase. Simply drag and drop, upload, or provide an image URL in the controls above and the encoder will quickly generate a Base64 encoded ...
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
URL to Base64 | Base64 Encode | Base64 Converter | Base64
To use it, just type or paste the URL and press “Encode URL to Base64” (make sure that the URL is publicly available). If you need more features, for better customization check the Base64 encoder. Please do not confuse this converter with the Base64URL Encode.
Discussions

javascript - Convert image from url to Base64
Using an Image File, I am getting the url of an image, that needs be to send to a webservice. From there the image has to be saved locally on my system. The code I am using: var imagepath = $("# More on stackoverflow.com
🌐 stackoverflow.com
How to convert image URI to base64. | OutSystems
I want to store the base64 data of Image URI in a local variable. Can anyone come up with some idea how I can do it · There's nothing out-of-the-box that supports this. If you google a bit you'll find ways to do that with JavaScript, e.g. here More on outsystems.com
🌐 outsystems.com
April 15, 2023
Way to convert image straight from URL to base64 without saving as a file in Python - Stack Overflow
I'm looking to convert a web-based image to base64. I know how to do it currently by saving the image as a .jpg file and then using the base64 library to convert the .jpg file to a base64 string. ... More on stackoverflow.com
🌐 stackoverflow.com
Is it a good idea to convert images into base64 string and save them in database?
You will get many people saying don't because if not implemented well it can bite ya. Don't be concerned about "optimal", tell yourself "good enough" and "don't let perfect be the enemy of good". But I have done this for several projects (enterprise, mission critical systems) and it works just fine. From the developer standpoint I love it. When I yank the "byte array" from the DB I can just pass it right to the client as-is, just adding on the mime type, super simple. Right now I have a system that stores photos of students at a university and we serve those out with an API. All done in C# and SQL Server. We have probably 50,000 photos and it is lighting fast. That being said I have also stored files in S3 and just stored the name in the DB. I will usually rename the file so S3 stores it efficiently. I like things like s3 better than a file system since you can dedicate a bucket just to this and some sysadmin won't go mucking around. I agree with you on storing the unstructured binary data along with your structured data for simplicity. I had a project once that wrote images to a filesystem. Years later older images were lost due to some file system cleanup process. Over my 25 years programming I have found you cannot guess where the issues will be. Don't optimize until you measure it. If you can build a proof of concept then hammer it, do it. Write a script that inserts and reads images and loop it for a few hours. See what happens. Also if you implement something and a few years later it starts to falter, then refactor. But you need to monitor! This is another place where people get bit, they do something like this and just let it run in a corner until it "just dies for no reason" one day. You or someone needs to care and feed for it. I am going to assume on-prem Microsoft SQL Server, your usage may vary. Use "VARBINARY(MAX)" for the field type https://docs.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver15 Put it in a table by itself with an id to look it up. Store filename, and filetype(mime-type) too if needed. Slap an index on the id, reference that id in other tables. Critical: Put the table in its own filegroup separate from the other tables. This will allow for faster restores since you can restore the primary filegroup and get your DB back online ASAP, then as it is up and running restore the filegroup with the images. So instead of the whole DB being down for hours, it is up in minutes with only the images being unavailable for hours. https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-files-and-filegroups?view=sql-server-ver15 As with all DBs the filegroups should not be on the system (C) drive, maybe even put the images table filegroup on its own drive with nothing else. Do set a limit on the incoming filesize in your code. Someone could send in a few Blu-ray ISOs and cripple your system. In .NET code you store the image data in a byte array "byte[] " variable. That byte array is what is stored in the varbinary(max) DB field. It is easy to convert the incoming image/file to byte[], just google around for that there are many easy to do it. Design and implement a cleanup process and purge old unneeded records. No one ever seems to do this and the data grows forever until the system is retired/migrated. For small DBs it's not a problem but for this you must go back to your stakeholder and get in writing when you can delete old records then set an automated process or scheduled manual progress to do it. If they need the data long term maybe older data can be moved out of the online system and to an archive or warehouse. Microsoft has some other methods that I have not used. Check those out. https://docs.microsoft.com/en-us/sql/relational-databases/blob/binary-large-object-blob-data-sql-server?view=sql-server-ver15 -- Good luck More on reddit.com
🌐 r/dotnet
111
46
April 28, 2022
🌐
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
🌐
Formspree
formspree.io › blog › image-to-base64
How to Convert an Image URL to Base64 | Formspree
August 28, 2024 - If you are looking to convert image URLs to Base64 images occasionally, an online Base64 encoder offers a quick and easy solution. These web-based tools typically require you to simply paste the image URL or upload the image file.
🌐
Base64 Image Encoder
base64-image.de
Convert Images to Base64 Online — Free Encoder & Optimizer | base64-image.de
Transform your images into base64-encoded strings instantly. Perfect for HTML, CSS, and email templates. Upload bigger files for free Sign in to convert images up to 5 MB per file.
🌐
OutSystems
outsystems.com › forums › discussion › 87490 › how-to-convert-image-uri-to-base64
How to convert image URI to base64. | OutSystems
April 15, 2023 - I want to store the base64 data of Image URI in a local variable. Can anyone come up with some idea how I can do it · There's nothing out-of-the-box that supports this. If you google a bit you'll find ways to do that with JavaScript, e.g. here
Find elsewhere
🌐
Browserling
browserling.com › tools › image-to-base64
Image to Base64 Converter - Encode Images to Base64 - Online - Browserling Web Developer Tools
Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64.
🌐
AskApache
askapache.com › / › askapache online tools › convert image base64 encoder
Convert Image Base64 Encoder - AskApache
September 18, 2022 - These can be included in CSS as ... requests. Background images can be encoded using the data URI scheme: url('data:image/png;base64, [data]) [ CSS ] Requires: RFC2397 data uri support....
🌐
npm
npmjs.com › package › image-to-base64
image-to-base64 - npm
imageToBase64("https://whateve... console.log(error); // Logs an error if there was one } ) You can import image-to-base64 using the <script> tag in HTML....
      » npm install image-to-base64
    
Published: Apr 09, 2021
Version: 2.2.0
🌐
Jam
jam.dev › utilities › image-to-base64
Image to Base64 Converter | Free, Open Source & Ad-free
Convert images to Base64 format quickly and easily with Jam's free online image to Base64 converter. Just drag and drop your image and get the Base64 result. That's it.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › URI › Reference › Schemes › data
data: URLs - URIs - MDN Web Docs - Mozilla
1 month ago - The scheme of the URL. ... The MIME type indicating the type of data, such as image/jpeg for a JPEG image file. If omitted, defaults to text/plain;charset=US-ASCII. You can find a full dissection of MIME type structure and a table of common MIME types on the web. ... Indicates that the data should be base64-decoded; see encoding data into base64 format.
🌐
Code Beautify
codebeautify.org › image-to-base64-converter
Image to Base64 converter to convert Image to Base64 String.
CSS background code of Image with base64 is also generated. The Image encoding tool supports loading the Image File to transform to Base64.
🌐
PixelPanda
pixelpanda.ai › home › free tools › image to base64
Image to Base64 — Convert Any Image to Base64 Online Free | PixelPanda
The format looks like this: data:image/png;base64,iVBORw0KGgo... You can use it anywhere you'd normally put an image URL -- img src attributes, CSS url() values, JavaScript Image objects.
🌐
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);
🌐
Riversun
riversun.github.io › image2base64
Convert image to base64 dataURL
You can drop a image file or paste image from clipboard to convert data URL · Drop here to add the file · The object cannot be dropped · OR · Select file · Data URL(base64) Image
🌐
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
Enable this option to encode into an URL- and filename- friendly Base64 variant (RFC 4648 / Base64URL) where the "+" and "/" characters are respectively replaced by "-" and "_", as well as the padding "=" signs are omitted.
🌐
Stack Abuse
stackabuse.com › bytes › converting-images-and-image-urls-to-base64-in-node-js
Converting Images and Image URLs to Base64 in Node.js
August 19, 2023 - In Node.js, converting an image URL to Base64 can be achieved using the request module along with the built-in Buffer class.