base64-2-img
v1.0.0
Published
A library for converting an image url or file to base64 string and base64 string back to an image file.
Downloads
205
Maintainers
Readme
Base64-2-img
base64-2-img is a light weight javascript image to base64
string and base64 string to image
conversion library for nodejs. This library implements the nodejs core http module to read an image blob into a base64 string.
Contents
Getting Started
This library is available through these javascript node package manager npm and yarn.
Installation
To use this library, first ensure you have a package manager initialized either with npm or yarn
# for npm use:
npm install --save base64-2-img
# for yarn use:
yarn add base64-2-img
To include image-2-base64 in your project. use one of these:
// ES6 and later
import { imageToBase64, base64ToImage } from "base64-2-img";
// or
import * as img2Base64 from "base64-2-img";
However, if you are using ECMAScript 5 and older, use the require statement:
// ES5 and older
var { imageToBase64, base64ToImage } = require("base64-2-img");
// or
var img2Base64 = require("base64-2-img");
Documentation
Img-2-base64 is a simple library with only two exposed functions. This is all intentional mainly to keep everything simple. This is a complete documentation of the library and how to use it in your project. All examples work on both ECMAScript 5 (ES5 and older).
Library Functions
| Function | Description | Parameter | Return | | ------------- | :----------------------------------------------------------------------------: | :---------------------------------------------------------------------: | -------------------------------------| | imageToBase64 | Accepts an image url or disk path return a base64 representation of the image | url - {string}: image uri or disk image file path | base64String - a base64 string | | base64ToImage | Accepts a base64 string and an option {object} and creates an image file (blob)| base64String - {string}: a base64 string representation of an image| success or Error - a promise that resolves to success if file was created or throws an error |
The base64ToImage(base64String[, option]) function accepts two arguments
base64String
- This is a required filed which is simply an image in base64 representation e.g/9j/4AAQSkZJRgABAQEASA...
option
- This is optional and can beone
orall
of the following propertiesfilePath
{string} - The directory path where the is to be generated defaults to theparent working directory
.fileName
{string} - The name of the file excluding the extensioin - the extension will be generated based on the base64 header if present or default to the nameimage.png
.randomizeFileName
{boolean} - Defaults tofalse
, if set totrue
this will use a randomized string as the image name e.g xHyu45.jpg.
Using the library
import { imageToBase64, base64ToImage } from "base64-2-img";
// using a remote image url - promises
var url = "http://image-file-path"
imageToBase64(url).then(function(_base64String) {
console.log(_base64String); // data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASA...
});
// using a local image file path - async-await
async function main() {
var url = path.resolve(__dirname, "..", "sampleImage.png");
var base64String = await imageToBase64(url);
var outputFilePath = path.resolve(__dirname, "temp");
base64ToImage(base64String, { filePath: outputFilePath, fileName: 'img' }); // success and create a file at temp/img.png or temp/img.jpg.
// or
var message = await base64ToImage(base64String, { filePath: outputFilePath, fileName: 'img' });
console.log(message); // success
}
...
NOTE: See the example folder for the full working code.
Contribution
To contribute, simply fork this project, and issue a pull request.
Version Management
We use SemVer for version management. For the versions available, see the tags on this repository.
Authors
Chukwuemeka Ajima - ajimae
License
This project is licensed under the MIT License - see the LICENSE file for details