An open API service indexing awesome lists of open source software.

https://github.com/harrystevens/ba64

A tiny npm module for saving Base64 encoded images that are part of data URLs to your file system.
https://github.com/harrystevens/ba64

Last synced: 18 days ago
JSON representation

A tiny npm module for saving Base64 encoded images that are part of data URLs to your file system.

Awesome Lists containing this project

README

        

# ba64
[![Build Status](https://travis-ci.org/HarryStevens/ba64.svg?branch=master)](https://travis-ci.org/HarryStevens/ba64) [![Coverage Status](https://coveralls.io/repos/github/HarryStevens/ba64/badge.svg?branch=master)](https://coveralls.io/github/HarryStevens/ba64?branch=master)

A tiny npm module for saving Base64 encoded images that are part of [data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) to your file system. This is useful for saving images that have been uploaded to the browser via [`FileReader.readAsDataUrl()`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL).

* [Installation](#installation)
* [Example](#example)
* [API](#api)

## Installation

```bash
npm i ba64 -S
```

## Example

```js
var ba64 = require("ba64"),
data_url = "data:image/jpeg;base64,[Base64 encoded image goes here]";

// Save the image synchronously.
ba64.writeImageSync("myimage", data_url); // Saves myimage.jpeg.

// Or save the image asynchronously.
ba64.writeImage("myimage", data_url, function(err){
if (err) throw err;

console.log("Image saved successfully");

// do stuff
});
```

## API

# ba64.**writeImage**(*path/to/file_name*, *data_url*, *callback*)

Asynchronously saves the Base64 encoded image to the file system. `file_name` should not include the file extension; ba64 will do that for you.

# ba64.**writeImageSync**(*path/to/file_name*, *data_url*)

Synchronously saves the Base64 encoded image to the file system. `file_name` should not include the file extension; ba64 will do that for you.

### Helper functions

# ba64.**getExt**(*data_url*)

Returns the file extension of the Base64 encoded image.

# ba64.**getBa64Img**(*data_url*)

Returns the Base64 encoded image without the `data:` scheme prefix.