Coder Social home page Coder Social logo

image-type's Introduction

image-type

Detect the image type of a Buffer/Uint8Array

See the file-type module for more file types and a CLI.

Install

npm install image-type

Usage

Node.js
import {readChunk} from 'read-chunk';
import imageType, {minimumBytes} from 'image-type';

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

await imageType(buffer);
//=> {ext: 'png', mime: 'image/png'}

Or from a remote location:

import https from 'node:https';
import imageType, {minimumBytes} from 'image-type';

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

https.get(url, response => {
	response.on('readable', () => {
		(async () => {
			const chunk = response.read(minimumBytes);
			response.destroy();
			console.log(await imageType(chunk));
			//=> {ext: 'jpg', mime: 'image/jpeg'}
		})();
	});
});
Browser
const xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';

xhr.onload = () => {
	(async () => {
		await imageType(new Uint8Array(this.response));
		//=> {ext: 'png', mime: 'image/png'}
	})();
};

xhr.send();

API

imageType(input)

Returns an Promise<object> with:

Or undefined when there is no match.

input

Type: Buffer | Uint8Array

It only needs the first minimumBytes amount of bytes.

minimumBytes

Type: number

The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.

Supported file types

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

Related

image-type's People

Contributors

bendingbender avatar jasonharrison avatar kevva avatar pgilad avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

image-type's Issues

Returning null when the buffer from a particular image link is used to get image type

Hi, when I make a HTTP GET request to this link https://cdn-sliders.eneba.com/QzxlwqBtARNYe7vb3hFE8XiV5NgPrtNcLGT7ZV2bAIU.jpeg and get its response as buffer and try to do imageType(buffer), it's returning null, I don't get why this happens, I've also checked a few images from that website (like 3) whose link is from cdn-sliders.eneba.com, all of their buffer is present but somehow, imageType(buffer) is always returning null for all of those links, I don't understand why this happens to these links from that website.

I've tested the same image in Discord's CDN by uploading it into Discord which gave me the link https://cdn.discordapp.com/attachments/764486125338230834/837739672654446622/QzxlwqBtARNYe7vb3hFE8XiV5NgPrtNcLGT7ZV2bAIU.jpeg which works totally fine and I was able to get the output successfully but getting null when I use the original link from that website, is there any reason behind this that anyone know of?

Update file-type dependency

This package depends on file-type v10 which is already a few major versions behind and marked as vulnerable by snyk. Note that I didn't verify if the vulnerability would really be a problem in reality. An update to the newest major version would still be nice though.

This package is e.g. used by image-js.

Some files fail the check

I have been using image-type for a while with great success. However, today I spotted that some files do not pass the check. Like this one

I have downloaded it and run in node repl:

> let rawImage
> let imageType
> const downloadImgBuffer = url => fetch(url).then(res => res.arrayBuffer()).then(data => Buffer.from(data)).then(image => { rawImage = image })
> import('image-type').then(m => { imgType = m.default })
> downloadImgBuffer('https://www.propertyfinder.ae/property/ff905549921f7bf7290837418d80b1cb/1312/894/MODE/588a2a/9568812-c8c42o.jpg?ctr=ae)
> imgType(rawImage).then(res => console.log(`this is the res: "${JSON.stringify(res)}"`))
this is the res: "false"

Exactly the same code with other images works just fine, ie this image correctly prints this is the res: "{"ext":"jpg","mime":"image/jpeg"}".

Checking on another website that gives image type, extension, mime etc: https://www.checkfiletype.com/ and dragging over there the failing image above gives correct image type:

File Type: image data, standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1312x894, frames 3
MIME Type: image/jpeg;
Suggested file extension(s): jpg jpeg jpe jfif

Furthermore, according to documentation the imageType function is supposed to return undefined if it fails to establish image type, but by the looks of the above it returns false.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.