image-size
get dimensions of any image file
Last updated 11 years ago by netroy .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install image-size 
SYNC missed versions from official npm registry.

image-size

Build Status Package Version Downloads

A Node module to get dimensions of any image file

Supported formats

  • BMP
  • CUR
  • DDS
  • GIF
  • ICNS
  • ICO
  • J2C
  • JP2
  • JPEG
  • KTX
  • PNG
  • PNM (PAM, PBM, PFM, PGM, PPM)
  • PSD
  • SVG
  • TGA
  • TIFF
  • WebP

Programmatic Usage

npm install image-size --save

or

yarn add image-size

Synchronous

const sizeOf = require('image-size')
const dimensions = sizeOf('images/funny-cats.png')
console.log(dimensions.width, dimensions.height)

Asynchronous

const sizeOf = require('image-size')
sizeOf('images/funny-cats.png', function (err, dimensions) {
  console.log(dimensions.width, dimensions.height)
})

NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.

Also, the asynchronous functions have a default concurrency limit of 100 To change this limit, you can call the setConcurrency function like this:

const sizeOf = require('image-size')
sizeOf.setConcurrency(123456)

Using promises (nodejs 10.x+)

const { promisify } = require('util')
const sizeOf = promisify(require('image-size'))
sizeOf('images/funny-cats.png')
  .then(dimensions => { console.log(dimensions.width, dimensions.height) })
  .catch(err => console.error(err))

Async/Await (Typescript & ES7)

const { promisify } = require('util')
const sizeOf = promisify(require('image-size'))
(async () => {
  try {
    const dimensions = await sizeOf('images/funny-cats.png')
    console.log(dimensions.width, dimensions.height)
  } catch (err) {
    console.error(err)
  }
})().then(c => console.log(c))

Multi-size

If the target file is an icon (.ico) or a cursor (.cur), the width and height will be the ones of the first found image.

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

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

Using a URL

const url = require('url')
const http = require('http')

const sizeOf = require('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(sizeOf(buffer))
  })
})

You can optionally check the buffer lengths & stop downloading the image after a few kilobytes. You don't need to download the entire image

Disabling certain image types

const imageSize = require('image-size')
imageSize.disableTypes(['tiff', 'ico'])

Disabling all file-system reads

const imageSize = require('image-size')
imageSize.disableFS(true)

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.

const sizeOf = require('image-size')
const dimensions = sizeOf('images/photo.jpeg')
console.log(dimensions.orientation)

Command-Line Usage (CLI)

npm install image-size --global

or

yarn global add image-size

followed by

image-size image1 [image2] [image3] ...

Hosted API

We also provide a hosted API for image-size which may simplify your use case.

Credits

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

Contributors

Current Tags

  • 2.0.0-alpha.1                                ...           alpha (2 years ago)
  • 1.0.2                                ...           latest (2 years ago)

59 Versions

  • 2.0.0-alpha.1                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           4 years ago
  • 0.9.7                                ...           4 years ago
  • 0.9.6                                ...           4 years ago
  • 0.9.5                                ...           4 years ago
  • 0.9.4                                ...           4 years ago
  • 0.9.3                                ...           4 years ago
  • 0.9.2                                ...           4 years ago
  • 0.9.1                                ...           4 years ago
  • 0.8.3                                ...           5 years ago
  • 0.8.2                                ...           5 years ago
  • 0.8.1                                ...           5 years ago
  • 0.8.0                                ...           5 years ago
  • 0.7.5                                ...           5 years ago
  • 0.7.4                                ...           6 years ago
  • 0.7.3                                ...           6 years ago
  • 0.7.2                                ...           6 years ago
  • 0.7.1                                ...           6 years ago
  • 0.7.0                                ...           6 years ago
  • 0.6.3                                ...           6 years ago
  • 0.6.2                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.5                                ...           8 years ago
  • 0.5.4                                ...           8 years ago
  • 0.5.3                                ...           8 years ago
  • 0.5.2                                ...           8 years ago
  • 0.5.1                                ...           8 years ago
  • 0.5.0                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.5                                ...           10 years ago
  • 0.3.3                                ...           10 years ago
  • 0.3.2                                ...           10 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           11 years ago
  • 0.2.5                                ...           11 years ago
  • 0.2.4                                ...           11 years ago
  • 0.2.3                                ...           11 years ago
  • 0.2.2                                ...           11 years ago
  • 0.2.1                                ...           11 years ago
  • 0.1.20                                ...           11 years ago
  • 0.1.17                                ...           11 years ago
  • 0.1.16                                ...           11 years ago
  • 0.0.16                                ...           11 years ago
  • 0.1.15                                ...           11 years ago
  • 0.1.10                                ...           11 years ago
  • 0.1.7                                ...           11 years ago
  • 0.1.6                                ...           11 years ago
  • 0.1.5                                ...           11 years ago
  • 0.1.4                                ...           11 years ago
  • 0.1.3                                ...           11 years ago
  • 0.1.2                                ...           11 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
  • 0.0.5                                ...           11 years ago
  • 0.0.3                                ...           11 years ago
  • 0.0.2                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 25
Last Day 0
Last Week 25
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2013 - present © cnpmjs.org | Home |