png-or-not
v1.0.0
Published
A lightweight utility to check whether a given file or buffer is a valid PNG image by verifying its signature bytes(magic number). Perfect for validating image uploads or streams before processing.
Downloads
6
Maintainers
Readme
png-or-not
A lightweight Node.js utility to check whether a given buffer contains a valid PNG image, by verifying its magic number (signature bytes).
📦 Installation
npm install png-or-not🚀 Usage
const fs = require('fs');
const isPng = require('png-or-not');
// Read a file into a buffer
const buffer = fs.readFileSync('image.png');
console.log(isPng(buffer)); // true if it's a PNG, false otherwise📘 API
isPng(buffer: Buffer): boolean
- Parameters:
buffer— a Node.js Buffer - Returns:
trueif the buffer starts with the PNG file signature,falseotherwise - Note: The function does not decode the image — it only checks the file signature
🧪 Testing
You can run the included test script:
npm testMake sure to place a valid sample.png file in the test/ directory.
📄 License
MIT © 2025 Abhay Patel
