# image-dimensions

> Get the dimensions of an image

Latest version **2.6.0** (published 2026-09-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install image-dimensions
pnpm add image-dimensions
yarn add image-dimensions
bun add image-dimensions
```

Provides the command `image-dimensions`.

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.6.0 |
| Published | 2026-09-18 |
| First published | 2015-04-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 23.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 602 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | image, dimensions, width, height, size, resolution, metadata, uint8array, buffer, data, bytes, binary, stream, cli, cli-app, png, jpeg, jpg, gif, webp, avif, heic, heif |

## Links

- npm: https://www.npmjs.com/package/image-dimensions
- Repository: https://github.com/sindresorhus/image-dimensions
- Homepage: https://github.com/sindresorhus/image-dimensions#readme
- Issues: https://github.com/sindresorhus/image-dimensions/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/image-dimensions

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 2.6.0 (latest) — 2026-09-18
- 2.5.1 — 2026-05-12
- 2.5.0 — 2025-09-08
- 2.4.0 — 2025-08-24
- 2.3.0 — 2023-12-19
- 2.2.0 — 2023-11-08
- 2.1.0 — 2023-11-06
- 2.0.0 — 2023-11-05
- 1.0.0 — 2015-04-14

## 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 are welcome.

## Supported formats

- JPEG
- PNG (and APNG)
- GIF
- WebP
- AVIF
- HEIF (including HEIC)

## 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, type: 'png'}
```

## API

`ImageType` is exported and can be one of: `'png'`, `'jpeg'`, `'gif'`, `'webp'`, `'avif'`, or `'heic'`.

### `imageDimensionsFromStream(stream: ReadableStream<Uint8Array>): Promise<{width: number; height: number; type: ImageType} | undefined>`

Get the dimensions of an image by reading the least amount of data.

Prefer this method.

Returns the image dimensions and type, or `undefined` if the image format is not supported or the image data is invalid.

Note: Returns raw pixel dimensions; orientation (EXIF or HEIF/AVIF `irot`) is not applied.

```js
// Node.js example
import {createReadStream} from 'node:fs';
import {Readable} from 'node:stream';
import {imageDimensionsFromStream} from 'image-dimensions';

const stream = Readable.toWeb(createReadStream('unicorn.png'));

console.log(await imageDimensionsFromStream(stream));
//=> {width: 1920, height: 1080, type: 'png'}
```

### `imageDimensionsFromData(data: Uint8Array): {width: number; height: number; type: ImageType} | 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 and type, or `undefined` if the image format is not supported or the image data is invalid.

Note: Returns raw pixel dimensions; orientation (EXIF or HEIF/AVIF `irot`) is not applied.

```js
import {imageDimensionsFromData} from 'image-dimensions';

const data = getImage();

console.log(imageDimensionsFromData(data));
//=> {width: 1920, height: 1080, type: 'png'}
```

## 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

**Advantages of `image-size`**

- 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

---
_Source: https://npm.io/package/image-dimensions · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
