1 | probe-image-size
|
2 | ================
|
3 |
|
4 | [![Build Status](https://img.shields.io/travis/nodeca/probe-image-size/master.svg?style=flat)](https://travis-ci.org/nodeca/probe-image-size)
|
5 | [![NPM version](https://img.shields.io/npm/v/probe-image-size.svg?style=flat)](https://www.npmjs.org/package/probe-image-size)
|
6 | [![Coverage Status](https://coveralls.io/repos/github/nodeca/probe-image-size/badge.svg?branch=master)](https://coveralls.io/github/nodeca/probe-image-size?branch=master)
|
7 |
|
8 | > Get image size without full download. Supported image types:
|
9 | > JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD.
|
10 |
|
11 | Key features:
|
12 |
|
13 | - small size, no heavy dependencies
|
14 | - works with remote and local data
|
15 | - effective with big images (speed/memory), download minimal data from remotes
|
16 | - easy to browserify (splitted to components)
|
17 |
|
18 |
|
19 | Install
|
20 | -------
|
21 |
|
22 | ```bash
|
23 | npm install probe-image-size --save
|
24 | ```
|
25 |
|
26 | Example
|
27 | -------
|
28 |
|
29 | ```js
|
30 | var probe = require('probe-image-size');
|
31 |
|
32 | // Get by URL
|
33 | //
|
34 | probe('http://example.com/image.jpg', function (err, result) {
|
35 | console.log(result); // =>
|
36 | /*
|
37 | {
|
38 | width: xx,
|
39 | height: yy,
|
40 | type: 'jpg',
|
41 | mime: 'image/jpeg',
|
42 | wUnits: 'px',
|
43 | hUnits: 'px'
|
44 | }
|
45 | */
|
46 | });
|
47 |
|
48 | // By URL with options
|
49 | //
|
50 | probe({ url: 'http://example.com/image.jpg', timeout: 5000 }, function (err, result) {
|
51 | console.log(result);
|
52 | });
|
53 |
|
54 | // With Promise
|
55 | //
|
56 | probe('http://example.com/image.jpg').then(function (result) {
|
57 | console.log(result);
|
58 | });
|
59 |
|
60 | // From the stream
|
61 | //
|
62 | var input = require('fs').createReadStream('image.jpg');
|
63 |
|
64 | probe(input, function (err, result) {
|
65 | console.log(result);
|
66 |
|
67 | // terminate input, depends on stream type,
|
68 | // this example is for fs streams only.
|
69 | input.destroy();
|
70 | });
|
71 |
|
72 | // From a Buffer
|
73 | //
|
74 | var data = require('fs').readFileSync('image.jpg');
|
75 |
|
76 | console.log(probe.sync(data));
|
77 | ```
|
78 |
|
79 |
|
80 | API
|
81 | ---
|
82 |
|
83 | ### probe(src [, callback(err, result)])
|
84 |
|
85 | `src` can be of this types:
|
86 |
|
87 | - __String__ - URL to fetch
|
88 | - __Object__ - options for [request](https://github.com/request/request),
|
89 | defaults are `{ timeout: 5000, maxRedirects: 2 }`
|
90 | - __Stream__ - readable stream
|
91 |
|
92 | `result` contains:
|
93 |
|
94 | ```js
|
95 | {
|
96 | width: XX,
|
97 | height: YY,
|
98 | length: ZZ, // byte length of the file (if available, HTTP only)
|
99 | type: ..., // image 'type' (usual file name extention)
|
100 | mime: ..., // mime type
|
101 | wUnits: 'px', // width units type ('px' by default, can be different for SVG)
|
102 | hUnits: 'px', // height units type ('px' by default, can be different for SVG)
|
103 | }
|
104 | ```
|
105 |
|
106 | `err` is an error, which is extended with:
|
107 |
|
108 | - `code` - equals to `ECONTENT` if the library failed to parse the file;
|
109 | - `status` - equals to a HTTP status code if it receives a non-200 response.
|
110 |
|
111 | If callback not provided, `Promise` will be returned.
|
112 |
|
113 | __Note.__ If you use `Stream` as source, it's your responsibility to close that
|
114 | stream in callback. In other case you can get memory leak, because stream will
|
115 | be left in paused state. With http requests that's not a problem - everything
|
116 | is released automatically, as soon as possible.
|
117 |
|
118 |
|
119 | ### sync.probe(src) -> result|null
|
120 |
|
121 | Sync version can eat arrays, typed arrays and buffers. On success it returns
|
122 | the same result as async version. On fail it returns null.
|
123 |
|
124 | __Note.__ Formats like JPEG & TIFF can store size anywhere (far from the head).
|
125 | That usually does not happens, but if you need guarantees - always provide full
|
126 | file content to sync methods. We strongly recommend to use async version
|
127 | as memory-friendly.
|
128 |
|
129 |
|
130 | Similar projects
|
131 | ----------------
|
132 |
|
133 | - [image-size](https://github.com/netroy/image-size)
|
134 | - [imagesize](https://github.com/arnaud-lb/imagesize.js)
|
135 |
|
136 |
|
137 | License
|
138 | -------
|
139 |
|
140 | [MIT](https://raw.github.com/nodeca/probe-image-size/master/LICENSE)
|