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 |
|
7 | A [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
|
29 | npm install image-size --save
|
30 | ```
|
31 |
|
32 | or
|
33 |
|
34 | ```shell
|
35 | yarn add image-size
|
36 | ```
|
37 |
|
38 | ### Synchronous
|
39 |
|
40 | ```javascript
|
41 | const sizeOf = require('image-size')
|
42 | const dimensions = sizeOf('images/funny-cats.png')
|
43 | console.log(dimensions.width, dimensions.height)
|
44 | ```
|
45 |
|
46 | ### Asynchronous
|
47 |
|
48 | ```javascript
|
49 | const sizeOf = require('image-size')
|
50 | sizeOf('images/funny-cats.png', function (err, dimensions) {
|
51 | console.log(dimensions.width, dimensions.height)
|
52 | })
|
53 | ```
|
54 |
|
55 | NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.
|
56 |
|
57 | Also, the asynchronous functions have a default concurrency limit of **100**
|
58 | To change this limit, you can call the `setConcurrency` function like this:
|
59 |
|
60 | ```javascript
|
61 | const sizeOf = require('image-size')
|
62 | sizeOf.setConcurrency(123456)
|
63 | ```
|
64 |
|
65 | ### Using promises (nodejs 10.x+)
|
66 |
|
67 | ```javascript
|
68 | const { promisify } = require('util')
|
69 | const sizeOf = promisify(require('image-size'))
|
70 | sizeOf('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
|
78 | const { promisify } = require('util')
|
79 | const 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 |
|
92 | 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.
|
93 |
|
94 | An additional `images` array is available and returns the dimensions of all the available images
|
95 |
|
96 | ```javascript
|
97 | const sizeOf = require('image-size')
|
98 | const images = sizeOf('images/multi-size.ico').images
|
99 | for (const dimensions of images) {
|
100 | console.log(dimensions.width, dimensions.height)
|
101 | }
|
102 | ```
|
103 |
|
104 | ### Using a URL
|
105 |
|
106 | ```javascript
|
107 | const url = require('url')
|
108 | const http = require('http')
|
109 |
|
110 | const sizeOf = require('image-size')
|
111 |
|
112 | const imgUrl = 'http://my-amazing-website.com/image.jpeg'
|
113 | const options = url.parse(imgUrl)
|
114 |
|
115 | http.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 |
|
126 | You 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
|
131 | const imageSize = require('image-size')
|
132 | imageSize.disableTypes(['tiff', 'ico'])
|
133 | ```
|
134 |
|
135 | ### Disabling all file-system reads
|
136 | ```javascript
|
137 | const imageSize = require('image-size')
|
138 | imageSize.disableFS(true)
|
139 | ```
|
140 |
|
141 | ## Command-Line Usage (CLI)
|
142 |
|
143 | ```shell
|
144 | npm install image-size --global
|
145 | ```
|
146 |
|
147 | or
|
148 |
|
149 | ```shell
|
150 | yarn global add image-size
|
151 | ```
|
152 |
|
153 | followed by
|
154 |
|
155 | ```shell
|
156 | image-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 |
|
169 | not 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)
|