Skip to content
Check if a Buffer/Uint8Array is a JPEG image
JavaScript
Branch: master
Clone or download
Latest commit 08df989 Oct 16, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig Require Node.js 6 Mar 22, 2018
.gitattributes Require Node.js 6 Mar 22, 2018
.gitignore Require Node.js 6 Mar 22, 2018
.npmrc Require Node.js 6 Mar 22, 2018
.travis.yml Require Node.js 6 Mar 22, 2018
fixture-imageoptim.jpg init Apr 21, 2014
fixture-issue1.jpg remove unnecessary check - fixes #1 Apr 25, 2014
fixture-tiny.jpg Add test for tiny JPEG image Oct 16, 2018
fixture.jpg init Apr 21, 2014
fixture.png init Apr 21, 2014
index.js Require Node.js 6 Mar 22, 2018
license Require Node.js 6 Mar 22, 2018
package.json 2.0.0 Mar 22, 2018
readme.md Require Node.js 6 Mar 22, 2018
test.js Add test for tiny JPEG image Oct 16, 2018

readme.md

is-jpg Build Status

Check if a Buffer/Uint8Array is a JPEG image

Install

$ npm install is-jpg

Usage

Node.js
const readChunk = require('read-chunk');
const isJpg = require('is-jpg');
const buffer = readChunk.sync('unicorn.jpg', 0, 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.

Related

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

License

MIT © Sindre Sorhus

You can’t perform that action at this time.