Created
November 14, 2015 03:40
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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