Skip to content

Instantly share code, notes, and snippets.

@Elements-
Created November 14, 2015 03:40
Show Gist options
  • Save Elements-/7457bbcaaba7dffeb3ad to your computer and use it in GitHub Desktop.
Save Elements-/7457bbcaaba7dffeb3ad 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