PNG.js
PNG.js is a PNG decoder fully written in JavaScript. It works in Node.js as well as in (modern) browsers.
Usage
var PNGReader = ;var reader = bytes;reader;
Or with options:
reader;
Currently the only option is:
data
(boolean) - should it read the pixel data, or only the image information.
PNG object
The PNG object is passed in the callback. It contains all the data extracted from the image.
// most importantlypng;png;png; // [red, blue, green, alpha]// but alsopng;png;png;png;png;png;
Using PNGReader in Node.js
PNGReader accepts an Buffer
object, returned by fs.readFile
, for example:
fs;
Using PNGReader in the Browser
PNGReader accepts a byte string, array of bytes or an ArrayBuffer.
For example using FileReader with file input fields:
var reader = ; reader{ var reader = eventtargetresult; reader;}; fileInputElement{ reader; // or, but less optimal reader;};
Or instead of using input elements, XHR can also be used:
var xhr = ;xhr;xhrresponseType = 'arraybuffer'; xhr{ if thisstatus == 200 var reader = thisresponse; reader; }; xhr;
Building Browser Version
PNG.js uses CommonJS modules which can be used in browsers after building it with browserify:
browserify ./PNGReader.js -s PNGReader