png-or-not
Version:
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.
53 lines (32 loc) โข 1.03 kB
Markdown
# 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
```bash
npm install png-or-not
```
## ๐ Usage
```js
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**: `true` if the buffer starts with the PNG file signature, `false` otherwise
- **Note**: The function does not decode the image โ it only checks the file signature
## ๐งช Testing
You can run the included test script:
```bash
npm test
```
Make sure to place a valid `sample.png` file in the `test/` directory.
## ๐ License
MIT ยฉ 2025 [Abhay Patel](mailto:patelabhay550@gmail.com)