1. is-jpg
Check if a Buffer/Uint8Array is a JPEG image
is-jpg
Package: is-jpg
Created by: sindresorhus
Last modified: Sun, 19 Jun 2022 02:44:11 GMT
Version: 3.0.0
License: MIT
Downloads: 2,582,876
Repository: https://github.com/sindresorhus/is-jpg

Install

npm install is-jpg
yarn add is-jpg

is-jpg

Check if a Buffer/Uint8Array is a JPEG image

Install

 npm install is-jpg

Usage

Node.js
 import {readChunk} from 'read-chunk';
import isJpg from 'is-jpg';

const buffer = await readChunk('unicorn.jpg', {length: 3});

isJpg(buffer);
//=> true
Browser
 const xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.jpg');
xhr.responseType = 'arraybuffer';

xhr.onload = () => {
	isJpg(new Uint8Array(this.response));
	//=> true
};

xhr.send();

API

isJpg(buffer)

Accepts a Buffer (Node.js) or Uint8Array.

It only needs the first 3 bytes.

  • file-type - Detect the file type of a Buffer/Uint8Array

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code