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.
- Host: GitHub
- URL: https://github.com/harrystevens/ba64
- Owner: HarryStevens
- License: mit
- Created: 2017-10-14T04:43:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T09:14:01.000Z (about 2 years ago)
- Last Synced: 2025-04-18T05:45:05.126Z (about 1 month ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/ba64
- Size: 58.6 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ba64
[](https://travis-ci.org/HarryStevens/ba64) [](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.