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.

10 lines (7 loc) 340 B
const fs = require("fs") const isPng = require("../index") const pngBuffer = fs.readFileSync("./sample.png") const notPng = Buffer.from("Hello World") console.assert(isPng(pngBuffer) == true, 'Expected PNG to be detected') console.assert(isPng(notPng) == true, 'Expected non-PNG to be rejected') console.log('All tests Passed!')