image-size vs imageinfo
图像处理库
image-sizeimageinfo类似的npm包:

图像处理库

图像处理库用于获取图像文件的元数据和尺寸信息,这在Web开发中非常重要,尤其是在处理图像上传、显示和优化时。通过使用这些库,开发者可以轻松获取图像的宽度、高度、格式和其他相关信息,从而提高应用程序的性能和用户体验。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
image-size21,929,3632,216378 kB441 年前MIT
imageinfo30,93259-415 年前-

功能对比: image-size vs imageinfo

功能范围

  • image-size:

    image-size库专注于获取图像的尺寸(宽度和高度),支持多种图像格式,如JPEG、PNG、GIF等。它的主要功能是快速、简洁地提取图像的基本尺寸信息,非常适合需要快速处理的应用。

  • imageinfo:

    imageinfo库提供更广泛的功能,不仅可以获取图像的尺寸,还可以提取图像的元数据,如文件类型、EXIF信息、颜色深度等。这使得它在需要详细图像分析和处理的场景中更具优势。

性能

  • image-size:

    image-size库的性能非常高效,能够快速读取图像文件并提取尺寸信息,适合高并发的场景。它的实现方式优化了内存使用和处理速度,适合需要快速响应的Web应用。

  • imageinfo:

    imageinfo库在性能上相对较慢,因为它需要解析更多的图像元数据。这使得它在处理大量图像时可能会成为瓶颈,但在需要详细信息的情况下,它提供的功能是无可替代的。

易用性

  • image-size:

    image-size库的API设计简单明了,易于使用。开发者只需调用一个函数即可获取图像的尺寸信息,非常适合初学者和快速开发。

  • imageinfo:

    imageinfo库的API相对复杂一些,因为它提供了更多的功能和选项。虽然功能强大,但对于初学者来说,可能需要更多的学习和理解。

支持的格式

  • image-size:

    image-size库支持多种常见的图像格式,如JPEG、PNG、GIF等。它的设计目标是覆盖大多数Web应用中使用的图像类型。

  • imageinfo:

    imageinfo库同样支持多种图像格式,但它还可以处理一些特殊格式,如TIFF和RAW图像。这使得它在专业图像处理领域更具优势。

社区与维护

  • image-size:

    image-size库有一个活跃的社区,定期更新和维护,确保其兼容性和性能。开发者可以轻松找到文档和支持。

  • imageinfo:

    imageinfo库的社区相对较小,但它的功能强大,适合需要深入图像分析的开发者。虽然更新频率较低,但其稳定性和功能性仍然值得信赖。

如何选择: image-size vs imageinfo

  • image-size:

    选择image-size库,如果你需要快速、简单地获取图像的尺寸信息,并且希望支持多种图像格式。它的API简单易用,非常适合需要快速处理图像尺寸的场景。

  • imageinfo:

    选择imageinfo库,如果你需要更全面的图像元数据,包括EXIF信息、颜色模式等。它适合需要深入分析图像内容和属性的应用场景。

image-size的README

image-size

Build Status Package Version Downloads

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

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

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 image-size
# or
yarn add image-size
# or
pnpm add 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 'image-size'
// or
const { imageSize } = require('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 'image-size/fromFile'
// or
const { imageSizeFromFile } = require('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 'image-size/fromFile'
// or
const { setConcurrency } = require('image-size/fromFile')
setConcurrency(123456)

Reading from a file Syncronously (not recommended) ⚠️

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 '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 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 'image-size/fromFile'
// or
const { imageSizeFromFile } = require('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 '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 'image-size'
// or
const { disableTypes } = require('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 'image-size/fromFile'
// or
const { imageSizeFromFile } = require('image-size/fromFile')

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

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

License

MIT

Credits

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

Contributors