Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/image-type
Detect the image type of a Buffer/Uint8Array
https://github.com/sindresorhus/image-type
Last synced: about 17 hours ago
JSON representation
Detect the image type of a Buffer/Uint8Array
- Host: GitHub
- URL: https://github.com/sindresorhus/image-type
- Owner: sindresorhus
- License: mit
- Created: 2014-04-21T17:46:50.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-06-26T12:20:09.000Z (6 months ago)
- Last Synced: 2024-12-03T20:25:56.083Z (9 days ago)
- Language: JavaScript
- Size: 339 KB
- Stars: 373
- Watchers: 10
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome - sindresorhus/image-type - Detect the image type of a Buffer/Uint8Array (JavaScript)
- awesome-nodejs-cn - image-type - 检测 Buffer / Uint8Array 的图像类型 (包 / 图像)
- awesome-nodejs - image-type - Detect the image type of a Buffer/Uint8Array. ![](https://img.shields.io/github/stars/sindresorhus/image-type.svg?style=social&label=Star) (Repository / Image)
- awesome-nodejs-cn - image-type - **star:373** 检测图像的类型。 (包 / 图像)
- awesome-nodejs - image-type - Detect the type of an image. (Packages / Image)
- awesome-nodejs - image-type - Detect the image type of a Buffer/Uint8Array - ★ 152 (Image)
- awesome-node - image-type - Detect the image type of a Buffer/Uint8Array. (Packages / Image)
- awesome-nodejs-cn - image-type - 检测Buffer / Uint8Array的图像类型. (目录 / 图像处理)
- awesome-nodejs - image-type - 检测图像类型 Buffer/Uint8Array (Uncategorized / Uncategorized)
README
# image-type
> Detect the image type of an ArrayBuffer/Uint8Array
See the [`file-type`](https://github.com/sindresorhus/file-type) module for more file types and a CLI.
## Install
```sh
npm install image-type
```## Usage
##### Node.js
```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:
```js
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
```js
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` with:
- `ext` - One of the [supported file types](#supported-file-types)
- `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type)Or `undefined` when there is no match.
#### input
Type: `ArrayBuffer | 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
- [`jpg`](https://en.wikipedia.org/wiki/JPEG)
- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)
- [`gif`](https://en.wikipedia.org/wiki/GIF)
- [`webp`](https://en.wikipedia.org/wiki/WebP)
- [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)
- [`cr2`](https://fileinfo.com/extension/cr2)
- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)
- [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)
- [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)
- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)
- [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`bpg`](https://bellard.org/bpg/)
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`heic`](https://nokiatech.github.io/heif/technical.html)
- [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*
## Related
- [file-type](https://github.com/sindresorhus/file-type) - Detect the type of a file
- [image-dimensions](https://github.com/sindresorhus/image-dimensions) - Get the dimensions of an image