pngjs-image
Advanced tools
Comparing version 0.11.2 to 0.11.3
CHANGELOG | ||
========= | ||
v0.11.3 - 05/23/15 | ||
* Add simple url support for readImage | ||
v0.11.2 - 04/21/15 | ||
@@ -5,0 +8,0 @@ * Bugfix for decoder: filter-revert from previous-line |
45
index.js
@@ -11,3 +11,5 @@ // Copyright 2014-2015, Yahoo! Inc. | ||
filters = require('./lib/filters'), | ||
streamBuffers = require("stream-buffers"); | ||
streamBuffers = require("stream-buffers"), | ||
MemoryStream = require('./lib/memoryStream'), | ||
request = require('request'); | ||
@@ -105,7 +107,14 @@ var Decoder = require('./lib/png/decoder'); | ||
* @method readImage | ||
* @param {string} filename | ||
* @param {string} path Url or file-path | ||
* @param {function} fn | ||
* @return {PNGImage} | ||
*/ | ||
PNGImage.readImage = function (filename, fn) { | ||
PNGImage.readImage = function (path, fn) { | ||
if (path.indexOf('http') === 0) { | ||
return this._readImageFromUrl(path, fn); | ||
} else { | ||
return this._readImageFromFs(path, fn); | ||
} | ||
}; | ||
PNGImage._readImageFromFs = function (filename, fn) { | ||
var image = new PNG(), | ||
@@ -128,3 +137,33 @@ resultImage = new PNGImage(image); | ||
}; | ||
PNGImage._readImageFromUrl = function (url, fn) { | ||
var stream, req; | ||
request.head(url, function (err, res) { | ||
var contentType = (res.headers['content-type'] || '').toLowerCase(); | ||
if (contentType !== 'image/png') { | ||
fn(new Error('Unsupported image format: ' + contentType)); | ||
} else { | ||
stream = new MemoryStream({size: res.headers['content-length']}); | ||
req = request(url).pipe(stream); | ||
req.on('error', function (err) { | ||
fn(err); | ||
}); | ||
req.on('finish', function () { | ||
var buffer = stream.getBuffer(); | ||
PNGImage.loadImage(buffer, fn); | ||
}); | ||
} | ||
}); | ||
return null; // This will be deprecated | ||
}; | ||
/** | ||
@@ -131,0 +170,0 @@ * Reads an image from the filesystem synchronously |
{ | ||
"name": "pngjs-image", | ||
"version": "0.11.2", | ||
"version": "0.11.3", | ||
"description": "Native PNG image manipulation", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -78,3 +78,4 @@ PNGjs-Image | ||
image.writeImage('path/to/file', function () { | ||
image.writeImage('path/to/file', function (err) { | ||
if (err) throw err; | ||
console.log('Written to the file'); | ||
@@ -86,3 +87,4 @@ }); | ||
```javascript | ||
var image = PNGImage.readImage('path/to/file', function () { | ||
PNGImage.readImage('path/to/file', function (err, image) { | ||
if (err) throw err; | ||
@@ -98,2 +100,11 @@ // Get width and height | ||
**Example:** Loading an image from an url | ||
```javascript | ||
PNGImage.readImage('https://s.yimg.com/rz/l/yahoo_en-US_f_p_142x37_2x.png', function (err, image) { | ||
if (err) throw err; | ||
// The image is in the 'image' variable if everything went well | ||
}); | ||
``` | ||
###Static-Methods | ||
@@ -103,4 +114,4 @@ * ```<PNGImage> = PNGImage.addFilter(key, fn)``` Adds the ```fn``` filter with identifier ```key``` to the filter-list | ||
* ```<PNGImage> = PNGImage.copyImage(image)``` Copies an image into a new container | ||
* ```<PNGImage> = PNGImage.readImage(path, fn)``` Loads an image from the filesystem, calling the ```fn``` function when done | ||
* ```<PNGImage> = PNGImage.loadImage(blob, fn)``` Loads an image from memory, calling the ```fn``` function when done | ||
* ```PNGImage.readImage(path, fn)``` Loads an image from the file or url, calling the ```fn``` function when done | ||
* ```PNGImage.loadImage(blob, fn)``` Loads an image from memory, calling the ```fn``` function when done | ||
@@ -186,2 +197,3 @@ ###Instance-Methods | ||
* underscore: http://underscorejs.org | ||
* request: https://github.com/request/request | ||
@@ -188,0 +200,0 @@ ###Dev-Dependencies |
98012
49
2952
210