UNPKG

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