Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
png.js
Advanced tools
Readme
PNG.js is a PNG decoder fully written in JavaScript. It works in Node.js as well as in (modern) browsers.
var PNGReader = require('png.js');
var reader = new PNGReader(bytes);
reader.parse(function(err, png){
if (err) throw err;
console.log(png);
});
Or with options:
reader.parse({
data: false
}, function(err, png){
if (err) throw err;
console.log(png);
});
Currently the only option is:
data
(boolean) - should it read the pixel data, or only the image information.The PNG object is passed in the callback. It contains all the data extracted from the image.
// most importantly
png.getWidth();
png.getHeight();
png.getPixel(x, y); // [red, blue, green, alpha]
// but also
png.getBitDepth();
png.getColorType();
png.getCompressionMethod();
png.getFilterMethod();
png.getInterlaceMethod();
png.getPalette();
PNGReader accepts an Buffer
object, returned by fs.readFile
, for example:
fs.readFile('test.png', function(err, buffer){
var reader = new PNGReader(buffer);
reader.parse(function(err, png){
if (err) throw err;
console.log(png);
});
});
PNGReader accepts a byte string, array of bytes or an ArrayBuffer.
For example using FileReader with file input fields:
var reader = new FileReader();
reader.onload = function(event){
var reader = new PNGReader(event.target.result);
reader.parse(function(err, png){
if (err) throw err;
console.log(png);
});
};
fileInputElement.onchange = function(){
reader.readAsArrayBuffer(fileInputElement.files[0]);
// or, but less optimal
reader.readAsBinaryString(fileInputElement.files[0]);
};
Or instead of using input elements, XHR can also be used:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'image.png', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e){
if (this.status == 200){
var reader = new PNGReader(this.response);
reader.parse(function(err, png){
if (err) throw err;
console.log(png);
});
}
};
xhr.send();
PNG.js uses CommonJS modules which can be used in browsers after building it with browserify:
browserify ./PNGReader.js -s PNGReader
FAQs
A PNG decoder fully written in JavaScript
The npm package png.js receives a total of 5,054 weekly downloads. As such, png.js popularity was classified as popular.
We found that png.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.