@carboneio/image-size

中文文档
3.0.1发布于 2026/10/15MITTypeScriptESM + CJS零依赖近 30 天下载 2798

get dimensions of any image file

原始使用文档
文档目录17 节
  1. Forked by Carbone
  2. Key Features
  3. Supported formats
  4. Installation
  5. Usage
  6. Passing in a Buffer/Uint8Array
  7. Reading from a file
  8. Reading from a file Syncronously (not recommended) ⚠️
  9. 3. Command Line
  10. Multi-size
  11. Using a URL
  12. Disabling certain image types
  13. JPEG image orientation
  14. Development
  15. License
  16. Credits
  17. Contributors

@carboneio/image-size

Fast, lightweight NodeJS package to get dimensions of any image file or buffer.

Forked by Carbone

Build and test NPM Version Downloads Code style: Biome Maintenance

After a thorough security audit (pentest) of our dependencies, we decided to fork this project (image-size on GitHub and image-size on Codeberg) to fix several vulnerabilities and, in the process, optimize the library. For a detailed list of changes, please see the full changelog.

We started by ensuring the test suite had 100% code coverage before making any changes. We also compared our results and performance with the tests from sharp, a widely used, more general-purpose library.

Speedup over image-size 2.0.2, per format, on a logarithmic scale

We would like to warmly thank the original developer of this library for their excellent work. We are maintaining this fork only because we needed to apply some urgent fixes and improvements; as soon as the original project incorporates these corrections, we will be very happy to remove this fork and return to the main repository.

Key Features

  • Zero dependencies
  • Supports all major image formats
  • Works with both files and buffers
  • Minimal memory footprint - reads only image headers
  • ESM and CommonJS support
  • TypeScript types included
  • Tested via GitHub Actions CI on Node.js 18, 20, 22, 24, and 26

Supported formats

  • BMP
  • CUR
  • DDS
  • GIF
  • HEIC (HEIF, AVCI, AVIF)
  • ICNS
  • ICO
  • J2C
  • JPEG-2000 (JP2)
  • JPEG
  • JPEG-XL
  • KTX (1 and 2)
  • PNG
  • PNM (PAM, PBM, PFM, PGM, PPM)
  • PSD
  • SVG
  • TGA
  • TIFF
  • WebP

Installation

npm install @carboneio/image-size
# or
yarn add @carboneio/image-size
# or
pnpm add @carboneio/image-size

Usage

Passing in a Buffer/Uint8Array

Best for streams, network requests, or when you already have the image data in memory.

import { imageSize } from '@carboneio/image-size'
// or
const { imageSize } = require('@carboneio/image-size')

const dimensions = imageSize(buffer)
console.log(dimensions.width, dimensions.height)

Reading from a file

Best for local files. Returns a promise.

import { imageSizeFromFile } from '@carboneio/image-size/fromFile'
// or
const { imageSizeFromFile } = require('@carboneio/image-size/fromFile')

const dimensions = await imageSizeFromFile('photos/image.jpg')
console.log(dimensions.width, dimensions.height)

Note: Reading from files has a default concurrency limit of 100 To change this limit, you can call the setConcurrency function like this:

import { setConcurrency } from '@carboneio/image-size/fromFile'
// or
const { setConcurrency } = require('@carboneio/image-size/fromFile')
setConcurrency(123456)

v1.x of this library had a sync API, that internally used sync file reads.

This isn't recommended because this blocks the node.js main thread, which reduces the performance, and prevents this library from being used concurrently.

However if you still need to use this package syncronously, you can read the file syncronously into a buffer, and then pass the buffer to this library.

import { readFileSync } from 'node:fs'
import { imageSize } from '@carboneio/image-size'

const buffer = readFileSync('photos/image.jpg')
const dimensions = imageSize(buffer)
console.log(dimensions.width, dimensions.height)

3. Command Line

Useful for quick checks.

npx @carboneio/image-size image1.jpg image2.png

Multi-size

If the target file/buffer is an HEIF, an ICO, or a CUR file, the width and height will be the ones of the largest image in the set.

An additional images array is available and returns the dimensions of all the available images

import { imageSizeFromFile } from '@carboneio/image-size/fromFile'
// or
const { imageSizeFromFile } = require('@carboneio/image-size/fromFile')

const { images } = await imageSizeFromFile('images/multi-size.ico')
for (const dimensions of images) {
  console.log(dimensions.width, dimensions.height)
}

Using a URL

import url from 'node:url'
import http from 'node:http'
import { imageSize } from '@carboneio/image-size'

const imgUrl = 'http://my-amazing-website.com/image.jpeg'
const options = url.parse(imgUrl)

http.get(options, function (response) {
  const chunks = []
  response
    .on('data', function (chunk) {
      chunks.push(chunk)
    })
    .on('end', function () {
      const buffer = Buffer.concat(chunks)
      console.log(imageSize(buffer))
    })
})

Disabling certain image types

import { disableTypes } from '@carboneio/image-size'
// or
const { disableTypes } = require('@carboneio/image-size')

disableTypes(['tiff', 'ico'])

JPEG image orientation

If the orientation is present in the JPEG EXIF metadata, it will be returned by the function. The orientation value is a number between 1 and 8 representing a type of orientation.

import { imageSizeFromFile } from '@carboneio/image-size/fromFile'
// or
const { imageSizeFromFile } = require('@carboneio/image-size/fromFile')

const { width, height, orientation } = await imageSizeFromFile('images/photo.jpeg')
console.log(width, height, orientation)

Performance

image-size reads a header; sharp decodes an image. That makes them hard to compare on anything but this one question: how long does it take to learn an image's dimensions? They give the same answer: across sharp's own 501 test fixtures, the two libraries agree on all 496 that both can read.

Measured against sharp 0.35.4 / libvips 8.18.6 on Node 24, macOS arm64.

From a buffer already in memory, parsing cost on its own:

FormatFixturesharpimage-sizeFactor
JPEG810 kB, 2725x2225101 µs0.48 µs209x
PNG6.8 MB, 2725x222576 µs0.34 µs226x
WebP173 kB, 1024x772206 µs0.52 µs400x
GIF279 kB, 800x53385 µs0.20 µs429x
TIFF249 kB, 246x345122 µs0.63 µs195x
AVIF279 kB, 2048x858102 µs1.39 µs74x
SVG32 kB, 480x360263 µs2.10 µs125x

From a file path, what an application actually pays, I/O included:

FormatSizesharpimage-sizeFactor
JPEG810 kB230 µs102 µs2.3x
PNG6.8 MB124 µs99 µs1.3x
WebP173 kB294 µs64 µs4.6x
GIF279 kB191 µs73 µs2.6x
TIFF249 kB285 µs69 µs4.1x
AVIF279 kB342 µs75 µs4.6x
SVG32 kB444 µs48 µs9.2x

The second table is the one to plan with. Reading the file dominates the cost, so the two orders of magnitude on a buffer shrink to between 1.3x and 9.2x once I/O is counted. image-size also has no native binary to install and no start-up cost, where sharp pays about 50 ms on its first call.

libvips' cache is disabled for these numbers, since the benchmark re-reads the same fixture in a loop and would otherwise measure memoisation rather than work. Files are warm in the OS page cache on both sides. Run npm run bench for throughput across all thirty formats.

Limitations

  1. Partial File Reading

    • Only reads image headers, not full files
    • Some corrupted images might still report dimensions
  2. SVG Limitations

    • Only supports pixel dimensions and viewBox
    • Percentage values not supported
  3. File Access

    • Reading from files has a default concurrency limit of 100
    • Can be adjusted using setConcurrency()
  4. Buffer Requirements

    • Some formats (like TIFF) require the full header in buffer
    • Streaming partial buffers may not work for all formats

Development

npm ci          # install dependencies from package-lock.json
npm test        # unit tests, with a 100% coverage threshold
npm run bench   # detection throughput, per format, on small and large files

npm run bench accepts --filter=<substring> to benchmark a single format, e.g. npm run bench -- --filter=webp.

License

MIT

Credits

not a direct port, but an attempt to have something like dabble's imagesize as a node module.

Contributors

版本与发布记录

@carboneio/image-size 共发布 2 个版本,首次发布于 2026/09/08;过去 12 个月发布了 2 个版本。

版本发布日期距今
3.0.1latest2026/10/1525 天前
3.0.02026/09/082 个月前

关于 @carboneio/image-size 的常见问题

@carboneio/image-size 是什么?

get dimensions of any image file

如何安装 @carboneio/image-size?

在项目目录运行 npm install @carboneio/image-size,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 image-size,可以用 npx image-size 直接运行。

@carboneio/image-size 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@carboneio/image-size 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@carboneio/image-size 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.x。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@carboneio/image-size 可以商用吗?

@carboneio/image-size 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carboneio/image-size 的下载量有多少?

根据 npm 官方下载统计,@carboneio/image-size 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,798 次,日均约 93 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carboneio/image-size 还在维护吗?

最新版本 3.0.1 发布于 2026/10/15(25 天前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。