UNPKG

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
# 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)