1 | # image-size
|
2 |
|
3 | [![NPM Version](https://img.shields.io/npm/v/image-size.svg)](https://www.npmjs.com/package/image-size)
|
4 | [![Build Status](https://travis-ci.org/image-size/image-size.svg?branch=master)](https://travis-ci.org/image-size/image-size)
|
5 | [![NPM Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=&from=&to=)
|
6 | [![Coverage Status](https://img.shields.io/coveralls/image-size/image-size/master.svg)](https://coveralls.io/github/image-size/image-size?branch=master)
|
7 | [![devDependency Status](https://david-dm.org/image-size/image-size/dev-status.svg)](https://david-dm.org/image-size/image-size#info=devDependencies)
|
8 |
|
9 | A [Node](https://nodejs.org/en/) module to get dimensions of any image file
|
10 |
|
11 | ## Supported formats
|
12 |
|
13 | * BMP
|
14 | * CUR
|
15 | * DDS
|
16 | * GIF
|
17 | * ICNS
|
18 | * ICO
|
19 | * JPEG
|
20 | * KTX
|
21 | * PNG
|
22 | * PNM (PAM, PBM, PFM, PGM, PPM)
|
23 | * PSD
|
24 | * SVG
|
25 | * TIFF
|
26 | * WebP
|
27 |
|
28 | ## Programmatic Usage
|
29 |
|
30 | ```shell
|
31 | npm install image-size --save
|
32 | ```
|
33 |
|
34 | ### Synchronous
|
35 |
|
36 | ```javascript
|
37 | var sizeOf = require('image-size');
|
38 | var dimensions = sizeOf('images/funny-cats.png');
|
39 | console.log(dimensions.width, dimensions.height);
|
40 | ```
|
41 |
|
42 | ### Asynchronous
|
43 |
|
44 | ```javascript
|
45 | var sizeOf = require('image-size');
|
46 | sizeOf('images/funny-cats.png', function (err, dimensions) {
|
47 | console.log(dimensions.width, dimensions.height);
|
48 | });
|
49 | ```
|
50 |
|
51 | NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.
|
52 |
|
53 | Also, the asynchronous functions have a default concurreny limit of **100**
|
54 | To change this limit, you can call the `setConcurrency` function like this:
|
55 |
|
56 | ```javascript
|
57 | var sizeOf = require('image-size');
|
58 | sizeOf.setConcurrency(123456)
|
59 | ```
|
60 |
|
61 | ### Using promises (nodejs 10.x+)
|
62 |
|
63 | ```javascript
|
64 | var { promisify } = require('util');
|
65 | var sizeOf = promisify(require('image-size'));
|
66 | sizeOf('images/funny-cats.png')
|
67 | .then(dimensions => { console.log(dimensions.width, dimensions.height); })
|
68 | .catch(err => console.error(err));
|
69 | ```
|
70 |
|
71 | ### Async/Await (Typescript & ES7)
|
72 |
|
73 | ```javascript
|
74 | var { promisify } = require('util');
|
75 | var sizeOf = promisify(require('image-size'));
|
76 | (async () => {
|
77 | try {
|
78 | const dimensions = await sizeOf('images/funny-cats.png');
|
79 | console.log(dimensions.width, dimensions.height);
|
80 | } catch (err) {
|
81 | console.error(err);
|
82 | }
|
83 | })().then(c => console.log(c));
|
84 | ```
|
85 |
|
86 | ### Multi-size
|
87 |
|
88 | 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.
|
89 |
|
90 | An additional `images` array is available and returns the dimensions of all the available images
|
91 |
|
92 | ```javascript
|
93 | var sizeOf = require('image-size');
|
94 | var images = sizeOf('images/multi-size.ico').images;
|
95 | for (const dimensions of images) {
|
96 | console.log(dimensions.width, dimensions.height);
|
97 | }
|
98 | ```
|
99 |
|
100 | ### Using a URL
|
101 |
|
102 | ```javascript
|
103 | var url = require('url');
|
104 | var http = require('http');
|
105 |
|
106 | var sizeOf = require('image-size');
|
107 |
|
108 | var imgUrl = 'http://my-amazing-website.com/image.jpeg';
|
109 | var options = url.parse(imgUrl);
|
110 |
|
111 | http.get(options, function (response) {
|
112 | var chunks = [];
|
113 | response.on('data', function (chunk) {
|
114 | chunks.push(chunk);
|
115 | }).on('end', function() {
|
116 | var buffer = Buffer.concat(chunks);
|
117 | console.log(sizeOf(buffer));
|
118 | });
|
119 | });
|
120 | ```
|
121 |
|
122 | You can optionally check the buffer lengths & stop downloading the image after a few kilobytes.
|
123 | **You don't need to download the entire image**
|
124 |
|
125 | ## Command-Line Usage (CLI)
|
126 |
|
127 | ```shell
|
128 | npm install image-size --global
|
129 | image-size image1 [image2] [image3] ...
|
130 | ```
|
131 |
|
132 | ## Hosted API
|
133 |
|
134 | We also provide a hosted API for image-size which may simplify your use case.
|
135 |
|
136 | <a href="https://image-size.saasify.sh">
|
137 | <img src="https://badges.saasify.sh?text=View%20Hosted%20API" height="40"/>
|
138 | </a>
|
139 |
|
140 | ## Credits
|
141 |
|
142 | not a direct port, but an attempt to have something like
|
143 | [dabble's imagesize](https://github.com/dabble/imagesize/blob/master/lib/image_size.rb) as a node module.
|
144 |
|
145 | ## [Contributors](Contributors.md)
|