Image to Base64 Encoder Online - Convert Image to Base64 String
I was recently asked by a reader how they couldbase64 encode an image in Node.js. There are a few ways to do this, but in this post we'll focus on the most common use case: encoding an image in Base64 format so it can be included in a data URL.
If you're not familiar with data URLs, they're basically a way to embed small files in web pages. Data URLs use a base64 encoding of the file data, so they're usually quite long. However, they have the advantage of being able to be used without making an additional HTTP request, which can be a big performance win in some cases.
In Node.js, there's a native Buffer class that provides a wrapper around the low-level memory allocation functions. This makes it easy to work with binary data in Node.js.
To base64 encode an image, we'll need the encoded data as a string. The simplest way to get this is to use the .toString('base64') method provided by the Buffer class.var fs = require('fs');
var data = fs.readFileSync('image.png');
var base64Image = data.toString('base64');
console.log(base64Image);
This will base64 encode the contents of image.png and print the resulting string to the console.
If you want to embed the image in a web page, you can use the base64 data as the value of the src attribute of an <img> tag:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QQEEi8F3vW2CQAAAYNJREFUOMvVk8trE1EUxr+7d7O0oqKgqoiCoKgr6ChYoi6+AgKCoqoi6i6uDg6Ojo6Ojo6ujoEQUS7Nzu7MzOzMzOzPzPzPzPz/z/M85xz/M8/w/MzOzMzOzMzOzOzOzOzOzOzMzOzMzOzOzMzOzOzOzOzOzOzMzOzMzOzMzOzMzOzMzOzOzMzOzMzOzOzMzOzMzOzMzOzMzOzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzMzOzM
This tool helps you to convert your Image to Base64 group with Ease.
All image formats are supported such as PNG, JPEG, GIF, etc.
The Image Upload limit is set to 4 MB.
Base64 encode image generates HTML code for IMG with Base64 as src (data source).
CSS background code of Image with base64 is also generated.
The Image encoding tool supports loading the Image File to transform to Base64. Click on the Upload Image button and select File.
Image to Base64 Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.
Comments
Post a Comment