What is is-png?
The is-png npm package is a utility that allows you to check if a given buffer or file is a PNG image. It is useful for validating image formats in various applications.
What are is-png's main functionalities?
Check if a buffer is a PNG
This feature allows you to check if a given buffer contains a PNG image. You can read a file into a buffer and then use the isPng function to validate its format.
const isPng = require('is-png');
const fs = require('fs');
const buffer = fs.readFileSync('path/to/image.png');
console.log(isPng(buffer)); // true if the buffer is a PNG, false otherwise
Check if a file is a PNG
This feature allows you to check if a given file is a PNG image by reading the file asynchronously and then using the isPng function to validate its format.
const isPng = require('is-png');
const fs = require('fs');
fs.readFile('path/to/image.png', (err, data) => {
if (err) throw err;
console.log(isPng(data)); // true if the file is a PNG, false otherwise
});
Other packages similar to is-png
image-type
The image-type package detects the file type of a buffer, supporting various image formats including PNG, JPEG, GIF, and more. It provides broader functionality compared to is-png, which is specific to PNG images.
file-type
The file-type package is a comprehensive utility for detecting the file type of a buffer or stream. It supports a wide range of file formats, including images, videos, documents, and more. It offers more extensive functionality compared to is-png, which is focused solely on PNG images.
is-jpg
The is-jpg package is similar to is-png but is specific to JPEG images. It allows you to check if a given buffer or file is a JPEG image, providing similar functionality but for a different image format.
is-png
Check if a Buffer/Uint8Array is a PNG image
Install
$ npm install is-png
Usage
Node.js
import {readChunk} from 'read-chunk';
import isPng from 'is-png';
const buffer = await readChunk('unicorn.png', {length: 8});
isPng(buffer);
Browser
import isPng from 'is-png';
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();
isPng(new Uint8Array(buffer));
API
isPng(buffer)
Accepts a Buffer (Node.js) or Uint8Array. Returns a boolean
of whether buffer
is a PNG image.
buffer
The buffer to check. It only needs the first 8 bytes.
Related
- file-type - Detect the file type of a Buffer/Uint8Array/ArrayBuffer