What is image-to-base64?
The image-to-base64 npm package is a simple utility that allows you to convert images to base64 strings. This can be useful for embedding images directly into HTML, CSS, or JSON, or for sending images over the network in a text-based format.
What are image-to-base64's main functionalities?
Convert Image URL to Base64
This feature allows you to convert an image from a URL to a base64 string. The code sample demonstrates how to use the package to fetch an image from a URL and convert it to a base64 string.
const imageToBase64 = require('image-to-base64');
imageToBase64('https://example.com/image.jpg') // Path to the image
.then((response) => {
console.log(response); // 'iVBORw0KGgoAAAANSUhEUgAA...'
})
.catch((error) => {
console.log(error);
});
Convert Local Image to Base64
This feature allows you to convert a local image file to a base64 string. The code sample demonstrates how to read a local image file using the 'fs' module and convert it to a base64 string using the image-to-base64 package.
const imageToBase64 = require('image-to-base64');
const fs = require('fs');
const imagePath = './path/to/local/image.jpg';
const imageBuffer = fs.readFileSync(imagePath);
imageToBase64(imageBuffer)
.then((response) => {
console.log(response); // 'iVBORw0KGgoAAAANSUhEUgAA...'
})
.catch((error) => {
console.log(error);
});
Other packages similar to image-to-base64
base64-img
The base64-img package provides similar functionality to image-to-base64, allowing you to convert images to base64 strings and vice versa. It also supports saving base64 strings as image files. Compared to image-to-base64, base64-img offers more flexibility with additional features for handling base64 strings and image files.
image-data-uri
The image-data-uri package allows you to convert images to data URIs and vice versa. It supports both local and remote images, similar to image-to-base64. However, image-data-uri focuses on creating data URIs, which include the base64 string along with the MIME type, making it more suitable for embedding images directly into HTML or CSS.
jimp
Jimp is an image processing library that includes functionality for converting images to base64 strings. In addition to base64 conversion, Jimp offers a wide range of image manipulation features such as resizing, cropping, and applying filters. Compared to image-to-base64, Jimp is a more comprehensive library for image processing tasks.
image-to-base64
Generate a base64 code from an image through a URL or a path.
About
It's a thing you can use in many situations, for example you can just save the base64 string in your database and increment it in the front-end with the <img>
tag in HTML.
Getting Started
Installation:
npm i -S image-to-base64
Code Example:
const imageToBase64 = require('image-to-base64');
imageToBase64("path/to/file.jpg")
.then(
(response) => {
console.log(response);
}
)
.catch(
(error) => {
console.log(error);
}
)
Remember that you can also use an image URL as a parameter.
Code Example:
imageToBase64("https://whatever-image/")
.then(
(response) => {
console.log(response);
}
)
.catch(
(error) => {
console.log(error);
}
)
Browser Usage
You can import image-to-base64 using the <script>
tag in HTML.
Code Example:
<script src="node_modules/image-to-base64/image-to-base64.min.js"></script>
Now you can use the module normally as in the JS examples above, but you can only use a URL and not a path.
LICENSE
MIT © 2017 RENAN.BASTOS