Skip to content
Library to easily decode/encode Data URI images
Branch: master
Clone or download
DiegoZoracKy Merge pull request #4 from joelabair/master
Fixes non-200 status code error handling
Latest commit af2fa12 Sep 21, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin added magicli + CLI installation instructions Nov 9, 2017
lib tests and fix options inclusion Sep 18, 2018
test tests and fix options inclusion Sep 18, 2018
.gitignore push to git Apr 1, 2016
.jshintrc push to git Apr 1, 2016
README.md added magicli + CLI installation instructions Nov 9, 2017
package.json Bump version to 2.0.0 Sep 21, 2018

README.md

Image Data URI

Library to easily decode/encode Data URI images

Installation

npm install image-data-uri

CLI

npm install image-data-uri -g
npx image-data-uri --help

Methods

  • decode(dataURI)
imageDataURI.decode('data:image/png;base64,SOMEPNGDATAURI/wD/')

// RETURNS
{
    imageType: 'png',
    dataBase64: 'SOMEPNGDATAURI/wD/',
    dataBuffer: <Buffer 89 50 4e 47 0d 0a ...>
}
  • encode(data, mediaType)
// Expects a Buffer of a image file
let dataBuffer = new Buffer(imageData);

// PNG | GIF | etc.
let mediaType = 'PNG';

// RETURNS :: image data URI :: 'data:image/png;base64,PNGDATAURI/wD/'
imageDataURI.encode(dataBuffer, mediaType)
  • encodeFromURL(imageURL)
// Returns a Promise
imageDataURI.encodeFromURL('http://www.some-site.com/some-image.png')

    // RETURNS image data URI :: 'data:image/png;base64,PNGDATAURI/'
    .then(res => console.log(res))
  • encodeFromFile(filePath)
// Returns a Promise
imageDataURI.encodeFromFile('./some-file.png')

    // RETURNS image data URI :: 'data:image/png;base64,PNGDATAURI/'
    .then(res => console.log(res))
  • outputFile(dataURI, filePath)
// Some image data uri
let dataURI = 'data:image/png;base64,PNGDATAURI/';

// It will create the full path in case it doesn't exist
// If the extension is defined (e.g. fileName.png), it will be preserved, otherwise the lib will try to guess from the Data URI
let filePath = './out/path/fileName';

// Returns a Promise
imageDataURI.outputFile(dataURI, filePath)

    // RETURNS image path of the created file 'out/path/fileName.png'
    .then(res => console.log(res))
You can’t perform that action at this time.