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

image-size

NPM Version Build Status NPM Downloads Coverage Status devDependency Status

A Node module to get dimensions of any image file

Supported formats

  • BMP
  • CUR
  • GIF
  • ICNS
  • ICO
  • JPEG
  • PNG
  • PSD
  • TIFF
  • WebP
  • SVG
  • DDS

Upcoming

  • SWF

Programmatic Usage

npm install image-size --save

Synchronous

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

Asynchronous

var 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.

Using promises (node 8.x)

var { promisify } = require('util');
var 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)

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

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

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

Using a URL

var url = require('url');
var http = require('http');

var sizeOf = require('image-size');

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

http.get(options, function (response) {
  var chunks = [];
  response.on('data', function (chunk) {
    chunks.push(chunk);
  }).on('end', function() {
    var 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

Command-Line Usage (CLI)

npm install image-size --global
image-size image1 [image2] [image3] ...

Credits

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

Contributors

Current Tags

  • 0.7.2                                ...           latest (3 days ago)

41 Versions

  • 0.7.2                                ...           3 days ago
  • 0.7.1                                ...           a month ago
  • 0.7.0                                ...           a month ago
  • 0.6.3                                ...           8 months ago
  • 0.6.2                                ...           a year ago
  • 0.6.1                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.5                                ...           2 years ago
  • 0.5.4                                ...           2 years ago
  • 0.5.3                                ...           2 years ago
  • 0.5.2                                ...           2 years ago
  • 0.5.1                                ...           2 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.5                                ...           4 years ago
  • 0.3.3                                ...           4 years ago
  • 0.3.2                                ...           5 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.5                                ...           5 years ago
  • 0.2.4                                ...           5 years ago
  • 0.2.3                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.1.20                                ...           5 years ago
  • 0.1.17                                ...           5 years ago
  • 0.1.16                                ...           5 years ago
  • 0.0.16                                ...           5 years ago
  • 0.1.15                                ...           5 years ago
  • 0.1.10                                ...           5 years ago
  • 0.1.7                                ...           5 years ago
  • 0.1.6                                ...           5 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
Downloads
Today 925
This Week 8,561
This Month 48,248
Last Day 7,636
Last Week 30,041
Last Month 192,472
Dependencies (0)
None
Dev Dependencies (7)
Dependents (727)

Copyright 2014 - 2017 © taobao.org |