npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

774

Readme

Base64-2-img

GitHub Workflow Status (branch) Coverage Status GitHub

GitHub release (latest by date including pre-releases) GitHub code size in bytes GitHub issues

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 be one or all of the following properties
    • filePath {string} - The directory path where the is to be generated defaults to the parent 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 name image.png.
    • randomizeFileName {boolean} - Defaults to false, if set to true 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