Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

image-type

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

image-type

Detect the image type of a Buffer/Uint8Array

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61K
decreased by-3.05%
Maintainers
2
Weekly downloads
 
Created
Source

image-type Build Status

Detect the image type of a Buffer/Uint8Array

See the file-type module for more file types.

Install

$ npm install --save image-type

Usage

Node.js
var readChunk = require('read-chunk'); // npm install read-chunk
var imageType = require('image-type');
var buffer = readChunk.sync('unicorn.png', 0, 12);

imageType(buffer);
//=> png

or from a remote location:

var http = require('http');
var imageType = require('image-type');
var url = 'http://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';

http.get(url, function (res) {
	res.once('data', function (chunk) {
		res.destroy();
		console.log(imageType(chunk));
		//=> gif
	});
});
Browser
var xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';

xhr.onload = function () {
	imageType(new Uint8Array(this.response));
	//=> png
};

xhr.send();

API

imageType(buffer)

Returns an object (or null when no match) with:

buffer

Type: buffer (Node.js), uint8array

It only needs the first 12 bytes.

CLI

$ npm install --global image-type
$ image-type --help

  Usage
    image-type <filename>
    cat <filename> | image-type

  Example
    cat unicorn.png | image-type
    png

Supported file types

  • jpg
  • png
  • gif
  • webp
  • tif
  • bmp
  • jxr
  • psd

SVG isn't included as it requires the whole file to be read, but you can get it here.

License

MIT © Sindre Sorhus

Keywords

FAQs

Package last updated on 19 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc