Skip to content

Commit 74bb0b7

Browse files
author
Shogun
committed
Improved API, added support for Buffers, promises and streams.
1 parent 2318a27 commit 74bb0b7

21 files changed

+2534
-399
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
node_modules
22
coverage.html
3-
tester.js
3+
examples.js
44
.idea

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### 2015-03-28 / 1.0.0
2+
3+
* Renamed `fastimage.analyze` as `fastimage.info`.
4+
* Added `fastimage.filteredInfo` to filter the result object.
5+
* Make `fastimage.info`, `fastimage.filteredInfo`, `fastimage.size` and `fastimage.type` return a Promise.
6+
* Added `fastimage.stream` and exported `FastImageStream` for streaming support.
7+
* Support for analyzing Buffers.
8+
* Added examples and test.
9+
110
### 2015-03-14 / 0.2.0 - The PI release!
211

312
* Added support for analyzing local files.

README.md

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ The supported image type are (thanks to the [image-size](https://github.com/netr
2323

2424
## Usage
2525

26+
For more details usage examples, see the `examples` folder.
27+
2628
### Callback style
2729

28-
To get the image informations, call `fastimage.analyze` passing the URL (can be also a local file path) and a callback.
30+
To get the image informations, call `fastimage.info` passing the URL (can be also a local file path or a Buffer) and a callback.
2931

3032
```javascript
3133
var fastimage = require("fastimage");
32-
fastimage.analyze("http://placehold.it/100x100", function(error, information){
34+
fastimage.info("http://placehold.it/100x100", function(error, information){
3335
if(error){
3436
// ...
3537
else{
@@ -38,26 +40,50 @@ fastimage.analyze("http://placehold.it/100x100", function(error, information){
3840
});
3941
```
4042
41-
For the details about the second parameter of the callback, see [fastimage.analyze](#user-content-fastimageanalyzeurl-callback),
42-
[fastimage.size](#user-content-fastimagesizeurl-callback) and [fastimage.type](#user-content-fastimagetypeurl-callback).
43+
For the details about the second parameter of the callback, see [fastimage.info](#user-content-fastimageinfosubject-callback),
44+
[fastimage.size](#user-content-fastimagesizesubject-callback) and [fastimage.type](#user-content-fastimagetypesubject-callback).
4345
4446
### Promise style
4547
46-
# TODO
48+
All the fastime image methods `info`, `filteredInfo`, `size` and `type` return a Promise. Once resolved, the promises have the same payload of the callbacks.
49+
50+
```javascript
51+
var fastimage = require("fastimage");
52+
53+
fastimage.info("http://placehold.it/100x100")
54+
.then(function(info){
55+
// ...
56+
})
57+
.catch(function(error){
58+
// ...
59+
});
60+
```
4761
4862
### Stream style
4963
50-
# TODO
64+
If you want to go *The Node.js Way*, you can use the streaming API.
65+
66+
Calling `fastimage.stream` it will return a Duplex stream object. Called without arguments, it's ideal for piping. Otherwise, you can use only it's readable side.
67+
68+
```javascript
69+
// Duplex
70+
request("http://placehold.it/100x100.png").pipe(fastimage.stream()).pipe(/*...*/);
71+
72+
// Readable
73+
fastimage.stream("http://placehold.it/100x100.png").pipe(/*...*/);
74+
```
75+
76+
Streams will emit the `size` and `type` if you only need those informations.
5177
5278
## Supported implementations.
5379
54-
Chalkbars supports and has been tested on [NodeJS](http://nodejs.org) 0.10+ and [io.js](http://iojs.org) 1.0+.
80+
Fastimage supports and has been tested on [NodeJS](http://nodejs.org) 0.10+ and [io.js](http://iojs.org) 1.0+.
5581
5682
## API Documentation
5783
58-
### fastimage.analyze(url, callback)
84+
### fastimage.info(subject, [callback])
5985
60-
Analyzes a URL (local or remote) and return the image informations.
86+
Analyzes a URL (local or remote) or a Buffer and return the image informations.
6187
6288
The signature of the callback is `function(error, info)`.
6389

@@ -94,26 +120,91 @@ When the URL is a local file the object will be similar to this:
94120
}
95121
```
96122
97-
### fastimage.size(url, callback)
123+
When the source is a Buffer the object will be similar to this:
98124
99-
Analyzes a URL (local or remote) and return the image size.
125+
```javascript
126+
{
127+
"width": 150, // The width of the image in pixels.
128+
"height": 150, // The height of the image in pixels.
129+
"type": "png", // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
130+
"analyzed": 4096 // The number of bytes analyzed.
131+
"time": 14.00558 // The time required for the operation, in milliseconds.
132+
}
133+
```
134+
135+
The function will return a Promise, which will resolve providing the information object.
136+
137+
### fastimage.size(subject, [callback])
138+
139+
Analyzes a URL (local or remote) or a Buffer and return the image size.
100140
101141
The signature of the callback is `function(error, dimensions)`.
102142

103143
The first argument of the callback, when failed, will be a [FastImageError](#user-content-fastimageFastImageError) instance.
104144

105145
The second argument of the callback, when successful, will be a object containing the fields `width` and `height` in pixels.
106146

107-
### fastimage.type(url, callback)
147+
The function will return a Promise, which will resolve providing the information object.
148+
149+
### fastimage.type(subject, [callback])
108150

109-
Analyzes a URL (local or remote) and return the image type.
151+
Analyzes a URL (local or remote) or a Buffer and return the image type.
110152

111153
The signature of the callback is `function(error, type)`.
112154

113155
The first argument of the callback, when failed, will be a [FastImageError](#user-content-fastimageFastImageError) instance.
114156

115157
The second argument of the callback, when successful, will be the type of the image.
116158

159+
The function will return a Promise, which will resolve providing the image type.
160+
161+
### fastimage.filteredInfo(subject, [filter, callback])
162+
163+
Analyzes a URL (local or remote) or a Buffer and return the image informations passing through a filter function.
164+
165+
The filter function should accept an object as input and return the object to be passed to the callback.
166+
167+
For details on the input object of the filter, see [fastimage.info](#user-content-fastimageinfosubject-callback).
168+
169+
The signature of the callback is `function(error, info)`.
170+
171+
The first argument of the callback, when failed, will be a [FastImageError](#user-content-fastimageFastImageError) instance.
172+
173+
The second argument of the callback, when successful, will be a object containing the image informations.
174+
175+
The function will return a Promise, which will resolve providing the filtered information object.
176+
177+
### fastimage.stream(subject, [options])
178+
179+
Creates a new fastimage stream analysis. This is a Duplex stream which works in object mode on the readable side.
180+
181+
It will emit the following events:
182+
183+
* **size**: The payload will be a object containing the fields `width` and `height` in pixels.
184+
* **type**: The payload will the type of the image.
185+
186+
### fastimage.timeout([timeout])
187+
188+
Gets or sets the maximum number of seconds to wait to connect to a host.
189+
190+
If the value is present, it will also set the new value. If the value is `null`, it will restore the default value.
191+
192+
The default value is `30000`.
193+
194+
### fastimage.threshold([threshold])
195+
196+
Gets or sets the maximum number of bytes to read to attempt an identification before giving up and state that the source is not an image.
197+
198+
If the value is present, it will also set the new value. If the value is `null`, it will restore the default value.
199+
200+
The default value is `4096`.
201+
202+
### fastimage.FastImageStream
203+
204+
A image analysis stream.
205+
206+
Streams will emit the `size` and `type` if you only need those informations about the image.
207+
117208
### fastimage.FastImageError
118209

119210
This error will be returned as the first argument of the callbacks if anything goes wrong.

docs/index.html

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,35 @@ <h3> </h3>
6161
<li>WebP</li>
6262
<li>SVG</li>
6363
</ul>
64-
<h2>Usage</h2><h3>Callback style</h3><p>To get the image informations, call <code>fastimage.analyze</code> passing the URL (can be also a local file path) and a callback.</p>
64+
<h2>Usage</h2><p>For more details usage examples, see the <code>examples</code> folder.</p>
65+
<h3>Callback style</h3><p>To get the image informations, call <code>fastimage.info</code> passing the URL (can be also a local file path or a Buffer) and a callback.</p>
6566
<pre class="prettyprint source lang-javascript"><code>var fastimage = require(&quot;fastimage&quot;);
66-
fastimage.analyze(&quot;http://placehold.it/100x100&quot;, function(error, information){
67+
fastimage.info(&quot;http://placehold.it/100x100&quot;, function(error, information){
6768
if(error){
6869
// ...
6970
else{
7071
// ...
7172
}
72-
});</code></pre><p>For the details about the second parameter of the callback, see <a href="#user-content-fastimageanalyzeurl-callback">fastimage.analyze</a>,
73-
<a href="#user-content-fastimagesizeurl-callback">fastimage.size</a> and <a href="#user-content-fastimagetypeurl-callback">fastimage.type</a>.</p>
74-
<h3>Promise style</h3><h1>TODO</h1><h3>Stream style</h3><h1>TODO</h1><h2>Supported implementations.</h2><p>Chalkbars supports and has been tested on <a href="http://nodejs.org">NodeJS</a> 0.10+ and <a href="http://iojs.org">io.js</a> 1.0+.</p>
75-
<h2>API Documentation</h2><h3>fastimage.analyze(url, callback)</h3><p>Analyzes a URL (local or remote) and return the image informations.</p>
73+
});</code></pre><p>For the details about the second parameter of the callback, see <a href="#user-content-fastimageinfosubject-callback">fastimage.info</a>,
74+
<a href="#user-content-fastimagesizesubject-callback">fastimage.size</a> and <a href="#user-content-fastimagetypesubject-callback">fastimage.type</a>.</p>
75+
<h3>Promise style</h3><p>All the fastime image methods <code>info</code>, <code>filteredInfo</code>, <code>size</code> and <code>type</code> return a Promise. Once resolved, the promises have the same payload of the callbacks.</p>
76+
<pre class="prettyprint source lang-javascript"><code>var fastimage = require(&quot;fastimage&quot;);
77+
78+
fastimage.info(&quot;http://placehold.it/100x100&quot;)
79+
.then(function(info){
80+
// ...
81+
})
82+
.catch(function(error){
83+
// ...
84+
});</code></pre><h3>Stream style</h3><p>If you want to go <em>The Node.js Way</em>, you can use the streaming API.</p>
85+
<p>Calling <code>fastimage.stream</code> it will return a Duplex stream object. Called without arguments, it's ideal for piping. Otherwise, you can use only it's readable side.</p>
86+
<pre class="prettyprint source lang-javascript"><code>// Duplex
87+
request(&quot;http://placehold.it/100x100.png&quot;).pipe(fastimage.stream()).pipe(/*...*/);
88+
89+
// Readable
90+
fastimage.stream(&quot;http://placehold.it/100x100.png&quot;).pipe(/*...*/);</code></pre><p>Streams will emit the <code>size</code> and <code>type</code> if you only need those informations.</p>
91+
<h2>Supported implementations.</h2><p>Fastimage supports and has been tested on <a href="http://nodejs.org">NodeJS</a> 0.10+ and <a href="http://iojs.org">io.js</a> 1.0+.</p>
92+
<h2>API Documentation</h2><h3>fastimage.info(subject, [callback])</h3><p>Analyzes a URL (local or remote) or a Buffer and return the image informations.</p>
7693
<p>The signature of the callback is <code>function(error, info)</code>.</p>
7794
<p>The first argument of the callback, when failed, will be a <a href="#user-content-fastimageFastImageError">FastImageError</a> instance.</p>
7895
<p>The second argument of the callback, when successful, will be a object containing the image informations.</p>
@@ -95,14 +112,45 @@ <h2>API Documentation</h2><h3>fastimage.analyze(url, callback)</h3><p>Analyzes a
95112
&quot;realPath&quot;: &quot;/home/user/1.png&quot;, // The absolute path of the image. It will be omitted if equals to the path.
96113
&quot;size&quot;: 24090, // The size (in bytes) of the image.
97114
&quot;time&quot;: 14.00558 // The time required for the operation, in milliseconds.
98-
}</code></pre><h3>fastimage.size(url, callback)</h3><p>Analyzes a URL (local or remote) and return the image size.</p>
115+
}</code></pre><p>When the source is a Buffer the object will be similar to this:</p>
116+
<pre class="prettyprint source lang-javascript"><code>{
117+
&quot;width&quot;: 150, // The width of the image in pixels.
118+
&quot;height&quot;: 150, // The height of the image in pixels.
119+
&quot;type&quot;: &quot;png&quot;, // The type of the image. Can be `bmp`, `gif`, `jpeg`, `png`, `psd`, `tif`, `webp` or `svg`.
120+
&quot;analyzed&quot;: 4096 // The number of bytes analyzed.
121+
&quot;time&quot;: 14.00558 // The time required for the operation, in milliseconds.
122+
}</code></pre><p>The function will return a Promise, which will resolve providing the information object.</p>
123+
<h3>fastimage.size(subject, [callback])</h3><p>Analyzes a URL (local or remote) or a Buffer and return the image size.</p>
99124
<p>The signature of the callback is <code>function(error, dimensions)</code>.</p>
100125
<p>The first argument of the callback, when failed, will be a <a href="#user-content-fastimageFastImageError">FastImageError</a> instance.</p>
101126
<p>The second argument of the callback, when successful, will be a object containing the fields <code>width</code> and <code>height</code> in pixels.</p>
102-
<h3>fastimage.type(url, callback)</h3><p>Analyzes a URL (local or remote) and return the image type.</p>
127+
<p>The function will return a Promise, which will resolve providing the information object.</p>
128+
<h3>fastimage.type(subject, [callback])</h3><p>Analyzes a URL (local or remote) or a Buffer and return the image type.</p>
103129
<p>The signature of the callback is <code>function(error, type)</code>.</p>
104130
<p>The first argument of the callback, when failed, will be a <a href="#user-content-fastimageFastImageError">FastImageError</a> instance.</p>
105131
<p>The second argument of the callback, when successful, will be the type of the image.</p>
132+
<p>The function will return a Promise, which will resolve providing the image type.</p>
133+
<h3>fastimage.filteredInfo(subject, [filter, callback])</h3><p>Analyzes a URL (local or remote) or a Buffer and return the image informations passing through a filter function.</p>
134+
<p>The filter function should accept an object as input and return the object to be passed to the callback.</p>
135+
<p>For details on the input object of the filter, see <a href="#user-content-fastimageinfosubject-callback">fastimage.info</a>.</p>
136+
<p>The signature of the callback is <code>function(error, info)</code>.</p>
137+
<p>The first argument of the callback, when failed, will be a <a href="#user-content-fastimageFastImageError">FastImageError</a> instance.</p>
138+
<p>The second argument of the callback, when successful, will be a object containing the image informations.</p>
139+
<p>The function will return a Promise, which will resolve providing the filtered information object.</p>
140+
<h3>fastimage.stream(subject, [options])</h3><p>Creates a new fastimage stream analysis. This is a Duplex stream which works in object mode on the readable side.</p>
141+
<p>It will emit the following events:</p>
142+
<ul>
143+
<li><strong>size</strong>: The payload will be a object containing the fields <code>width</code> and <code>height</code> in pixels.</li>
144+
<li><strong>type</strong>: The payload will the type of the image.</li>
145+
</ul>
146+
<h3>fastimage.timeout([timeout])</h3><p>Gets or sets the maximum number of seconds to wait to connect to a host.</p>
147+
<p>If the value is present, it will also set the new value. If the value is <code>null</code>, it will restore the default value.</p>
148+
<p>The default value is <code>30000</code>.</p>
149+
<h3>fastimage.threshold([threshold])</h3><p>Gets or sets the maximum number of bytes to read to attempt an identification before giving up and state that the source is not an image.</p>
150+
<p>If the value is present, it will also set the new value. If the value is <code>null</code>, it will restore the default value.</p>
151+
<p>The default value is <code>4096</code>.</p>
152+
<h3>fastimage.FastImageStream</h3><p>A image analysis stream.</p>
153+
<p>Streams will emit the <code>size</code> and <code>type</code> if you only need those informations about the image.</p>
106154
<h3>fastimage.FastImageError</h3><p>This error will be returned as the first argument of the callbacks if anything goes wrong.</p>
107155
<p>It always have the <code>message</code> and <code>code</code> fields set.</p>
108156
<h2>Contributing to fastimage</h2><ul>
@@ -125,13 +173,13 @@ <h2>Copyright</h2><p>Copyright (C) 2015 and above Shogun (shogun@cowtech.it).</p
125173
</div>
126174

127175
<nav>
128-
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fastimage.html">fastimage</a></li></ul><h3>Classes</h3><ul><li><a href="module-fastimage.FastImageError.html">FastImageError</a></li></ul>
176+
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fastimage.html">fastimage</a></li></ul><h3>Classes</h3><ul><li><a href="module-fastimage.FastImageError.html">FastImageError</a></li><li><a href="module-fastimage.FastImageStream.html">FastImageStream</a></li></ul>
129177
</nav>
130178

131179
<br class="clear">
132180

133181
<footer>
134-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 14 2015 16:00:39 GMT-0700 (PDT)
182+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta2</a> on Sat Mar 28 2015 14:49:38 GMT-0700 (PDT)
135183
</footer>
136184

137185
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)