Permalink
Browse files

Merge branch 'url-load'

  • Loading branch information...
marcelerz committed May 23, 2015
2 parents 334c55e + c891524 commit 8a13741999d0e1d131464296b3356dee41756198
Showing with 115 additions and 8 deletions.
  1. +4 −1 CHANGELOG.md
  2. +16 −4 README.md
  3. +14 −0 examples/readFromUrl.js
  4. +42 −3 index.js
  5. +39 −0 lib/memoryStream.js
View
@@ -1,12 +1,15 @@
CHANGELOG
=========
v0.11.3
v0.11.4
* Refactor decoder - fully dynamic
* Complete encoder - also fully dynamic
* Add support for custom chunks
* Add instrumentation for require
v0.11.3 - 05/23/15
* Add simple url support for readImage
v0.11.2 - 04/21/15
* Bugfix for decoder: filter-revert from previous-line
View
@@ -75,14 +75,16 @@ console.log(image.getRed(index));
// Get low level image object with buffer from the 'pngjs' package
var pngjs = image.getImage();
image.writeImage('path/to/file', function () {
image.writeImage('path/to/file', function (err) {
if (err) throw err;
console.log('Written to the file');
});
```
**Example:** Loading an image
```javascript
var image = PNGImage.readImage('path/to/file', function () {
PNGImage.readImage('path/to/file', function (err, image) {
if (err) throw err;
// Get width and height
console.log(image.getWidth());
@@ -93,12 +95,21 @@ var image = PNGImage.readImage('path/to/file', function () {
});
```
**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
* ```<PNGImage> = PNGImage.addFilter(key, fn)``` Adds the ```fn``` filter with identifier ```key``` to the filter-list
* ```<PNGImage> = PNGImage.createImage(width, height)``` Creates an image with the given size
* ```<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
###Instance-Methods
* ```<pngjs> = image.getImage()``` Gets the ```pngjs``` instance
@@ -179,6 +190,7 @@ The following third-party libraries are used by this module:
* pngjs: https://github.com/niegowski/node-pngjs
* stream-buffers: https://github.com/samcday/node-stream-buffer
* underscore: http://underscorejs.org
* request: https://github.com/request/request
###Dev-Dependencies
* chai: http://chaijs.com
View
@@ -0,0 +1,14 @@
// Copyright 2015, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
var PNGImage = require('../index');
PNGImage.readImage('https://s.yimg.com/rz/l/yahoo_en-US_f_p_142x37_2x.png', function (err, image) {
if (err) throw err;
image.writeImage(__dirname + '/url_image.png', function (err) {
if (err) throw err;
console.log('done');
});
});
View
@@ -8,7 +8,9 @@ var fs = require('fs'),
modify = require('./lib/modify'),
conversion = require('./lib/conversion'),
filters = require('./lib/filters'),
streamBuffers = require("stream-buffers");
streamBuffers = require("stream-buffers"),
MemoryStream = require('./lib/memoryStream'),
request = require('request');
var Decoder = require('./lib/png/decoder');
var Encoder = require('./lib/png/encoder');
@@ -101,11 +103,18 @@ PNGImage.copyImage = function (image) {
*
* @static
* @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(),
resultImage = new PNGImage(image);
@@ -123,6 +132,36 @@ PNGImage.readImage = function (filename, fn) {
return resultImage;
};
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
};
/**
* Reads an image from the filesystem synchronously
View
@@ -0,0 +1,39 @@
var stream = require('stream');
var util = require('util');
var MemoryStream = function (options) {
options = options || {};
stream.Writable.call(this, options);
this.buffer = new Buffer((options.size || 16 * 1024) * 1);
this.offset = 0;
};
util.inherits(MemoryStream, stream.Writable);
MemoryStream.prototype._write = function (chunk, encoding, cb) {
cb = cb || function () {};
var localBuffer = new Buffer(chunk, encoding);
// Need to resize memory?
if (this.buffer.length - this.offset < localBuffer.length) {
var buffer = new Buffer(this.buffer.length + localBuffer.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
}
localBuffer.copy(this.buffer, this.offset, 0, localBuffer.length);
this.offset += localBuffer.length;
cb(null, localBuffer);
};
MemoryStream.prototype.getBuffer = function () {
var localBuffer = new Buffer(this.offset);
this.buffer.copy(localBuffer, 0, 0, this.offset);
return localBuffer;
};
module.exports = MemoryStream;

0 comments on commit 8a13741

Please sign in to comment.