Skip to content

Instantly share code, notes, and snippets.

@aameralduais
Forked from Elements-/pngHeaderInformation.js
Created October 11, 2017 08:42
Show Gist options
  • Save aameralduais/a39474a47237cb729e78b4c77e16cdfe to your computer and use it in GitHub Desktop.
Save aameralduais/a39474a47237cb729e78b4c77e16cdfe to your computer and use it in GitHub Desktop.
Get PNG header information from an image (width, height, bit depth, and color type)
var fs = require('fs');
fs.readFile('test.png', function (err, data) {
var pngHeader = new Buffer([137, 80, 78, 71, 13, 10, 26, 10]);
var header = data.slice(0, 8);
if(header.compare(pngHeader) != 0) {
console.log('Invalid png file header!')
process.exit();
}
var fileHeader = {
"width" : data.readInt32BE(16),
"height" : data.readInt32BE(20),
"bitDepth" : data.readInt32BE(24),
"colorType" : data.readInt32BE(28)
}
console.log(fileHeader);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment