New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

imghdr

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

imghdr

Determines the type of image.

  • 0.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

imghdr

Build Status NPM Version NPM Downloads

The imghdr module determines the type of image contained in a file or octet-streams(object of Buffer), inspired by Python's imghdr module.

Installation

npm install imghdr

Usage

var imghdr = require('imghdr');

var imgPath = __dirname + '/meituan.jpg';
var ext = 'jpg';

var exts = imghdr.what(imghdr);
console.log(exts);
// => ['jpg', 'jpeg']

if (exts.indexOf(ext) === -1) {
    console.log('The `' + imgPath + '`\'s extension is not a `' + ext + '`');
} else {
    console.log('OK');
}
// => 'OK'

API

The imghdr module defines the following function:

imghdr.what(imgPath)

Tests the image data contained in the file named by imgPath, and returns an array of strings describing the image type. imgPath can be a object of Buffer.

ValueImage format
"png"Portable Network Graphics
"jpeg","jpg"JPEG data in JFIF or Exif formats
"gif"GIF 87a and 89a Files
"tiff"Tagged Image File Format
"bmp"BMP files
"webp"WebP files

You can extend the list of file types imghdr can recognize by appending to this variable:

imghdr.tests

A list of functions performing the individual tests. Each function takes a argument: the octet-streams(object of Buffer).The test function should return an array of strings describing the image type if the test succeeded, or [] if it failed.

Example:

function testCustom(buf) {
    var sigBuf = new Buffer([0x6c, 0x6f, 0x76, 0x65]);
    var testSigBuf = buf.slice(2, 6);

    return (sigBuf.toString() == testSigBuf.toString()) ? ['custom'] : [];
}

// add to list of `tests`
imghdr.tests.push(testCustom);

var testBuf = new Buffer([0x49, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, 0x79, 0x6f, 0x75]);
imghdr.what(testBuf);
// => ['custom']

Reference

LICENSE

MIT

Keywords

FAQs

Package last updated on 25 Mar 2015

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