Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
The pngjs npm package is a pure JavaScript implementation for PNG encoding and decoding in Node.js. It allows for reading and writing PNG images in a non-blocking and streaming way, which is useful for applications that need to handle image data directly.
Reading PNG files
This code demonstrates how to read a PNG file using pngjs. It creates a read stream from a file, pipes it through the PNG decoder, and logs the image's width and height once it's parsed.
const fs = require('fs');
const PNG = require('pngjs').PNG;
fs.createReadStream('input.png')
.pipe(new PNG())
.on('parsed', function() {
console.log('Image width:', this.width, 'Image height:', this.height);
});
Writing PNG files
This code snippet shows how to create a new PNG image with a specific width and height, fill it with white color, and then write it to a file. It demonstrates the package's ability to write PNG files.
const fs = require('fs');
const PNG = require('pngjs').PNG;
const png = new PNG({ width: 10, height: 10, filterType: -1 });
for (let y = 0; y < png.height; y++) {
for (let x = 0; x < png.width; x++) {
let idx = (png.width * y + x) << 2;
png.data[idx] = 255; // red
png.data[idx + 1] = 255; // green
png.data[idx + 2] = 255; // blue
png.data[idx + 3] = 255; // alpha (opacity)
}
}
png.pack().pipe(fs.createWriteStream('output.png'));
Sharp is a high-performance Node.js image processing library. It allows for converting large images in common formats to smaller, web-friendly JPEG, PNG, WebP, and AVIF images of varying dimensions. Compared to pngjs, sharp supports a wider range of image formats and provides more comprehensive image processing capabilities, but it relies on native dependencies.
Jimp is an image processing library for Node.js, written entirely in JavaScript, with zero native dependencies. It can perform a variety of operations such as resize, crop, rotate, and color manipulation. While Jimp supports PNG among other formats, it offers broader functionality beyond just PNG encoding and decoding, making it more versatile than pngjs for general image processing tasks.
Simple PNG encoder/decoder for Node.js with no native dependencies.
$ npm install pngjs
var fs = require('fs'),
PNG = require('pngjs').PNG;
var png = new PNG({
filterType: 4
}),
src = fs.createReadStream(process.argv[2]),
dst = fs.createWriteStream(process.argv[3]);
png.on('parsed', function() {
for (var y = 0; y < png.height; y++) {
for (var x = 0; x < png.width; x++) {
var idx = (png.width * y + x) << 2;
// invert color
png.data[idx] = 255 - png.data[idx];
png.data[idx+1] = 255 - png.data[idx+1];
png.data[idx+2] = 255 - png.data[idx+2];
// and reduce opacity
png.data[idx+3] = png.data[idx+3] >> 1;
}
}
png.pack();
});
src.pipe(png).pipe(dst);
For more examples see examples
folder.
Currently only true color mode with 8-bit color depth (per color) with alpha is supported. PNG cannot parse and create images with palette of colors. Interlaced mode is not supported either.
PNG
is readable and writeable Stream
.
width
- int
height
- int
checkCRC
- boolean
default: true
deflateChunkSize
- int
default: 32 kBdeflateLevel
- int
default: 9filterType
- int
default: -1 (auto)function(data) { }
Starts converting data to PNG file Stream
Parses PNG file data. Alternatively you can stream data to PNG.
Optional callback
is once called on error
or parsed
. The callback gets
two arguments (err, data)
.
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity).
(The MIT License)
Copyright (c) 2012 Kuba Niegowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
PNG encoder/decoder in pure JS, supporting any bit size & interlace, async & sync with full test suite.
The npm package pngjs receives a total of 7,703,068 weekly downloads. As such, pngjs popularity was classified as popular.
We found that pngjs 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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.