Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/image-dimensions
Get the dimensions of an image
https://github.com/sindresorhus/image-dimensions
Last synced: 7 days ago
JSON representation
Get the dimensions of an image
- Host: GitHub
- URL: https://github.com/sindresorhus/image-dimensions
- Owner: sindresorhus
- License: mit
- Created: 2023-11-05T15:20:19.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-21T23:45:21.000Z (6 months ago)
- Last Synced: 2024-12-01T01:25:39.528Z (11 days ago)
- Language: JavaScript
- Homepage:
- Size: 135 KB
- Stars: 433
- Watchers: 4
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-nodejs-cn - image-dimensions - **star:430** 获取图像的尺寸。 (包 / 图像)
- awesome-nodejs - image-dimensions - Get the dimensions of an image. (Packages / Image)
README
# image-dimensions
> Get the dimensions of an image
*Works in any modern JavaScript environment (browsers, Node.js, Bun, Deno, etc).*
Supporting all kinds of image formats is a non-goal. However, pull requests for adding JPEG XL and HEIC formats are welcome.
## Supported formats
- JPEG
- PNG (and APNG)
- GIF
- WebP
- AVIF## Install
```sh
npm install image-dimensions
```## Usage
```js
import {imageDimensionsFromStream} from 'image-dimensions';// In this example, it will only read a few bytes of the image instead of fetching the whole thing.
const url = 'https://sindresorhus.com/unicorn';
const {body} = await fetch(url);
console.log(await imageDimensionsFromStream(body));
//=> {width: 1920, height: 1080}
```## API
### `imageDimensionsFromStream(stream: ReadableStream): Promise<{width: number; height: number} | undefined>`
Get the dimensions of an image by reading the least amount of data.
Prefer this method.
Returns the image dimensions, or `undefined` if the image format is not supported or the image data is invalid.
```js
// Node.js example
import {createReadStream} from 'node:fs';
import {imageDimensionsFromStream} from 'image-dimensions';const stream = ReadableStream.from(createReadStream('unicorn.png'));
console.log(await imageDimensionsFromStream(stream));
//=> {width: 1920, height: 1080}
```### `imageDimensionsFromData(data: Uint8Array): {width: number; height: number} | undefined`
Get the dimensions of an image from data.
This method can be useful if you already have the image loaded in memory.
Returns the image dimensions, or `undefined` if the image format is not supported or the image data is invalid.
```js
import {imageDimensionsFromData} from 'image-dimensions';const data = getImage();
console.log(imageDimensionsFromData(data));
//=> {width: 1920, height: 1080}
```## CLI
```sh
npx image-dimensions unicorn.png
630x400
```## FAQ
### How does this differ from [`image-size`](https://github.com/image-size/image-size)?
**Advantages of this package**
- Zero dependencies
- Smaller
- Works in non-Node.js environments like the browser
- Does not include unnecessary APIs for file reading
- Supports the AVIF image format**Advantages of `image-size`**
- More mature
- Supports more image formats
- Supports getting JPEG image orientation## Related
- [image-type](https://github.com/sindresorhus/image-type) - Detect the type of an image
- [file-type](https://github.com/sindresorhus/file-type) - Detect the type of a file