Skip to content

Commit

Permalink
added; handle Buffer as source
Browse files Browse the repository at this point in the history
  • Loading branch information
danmilon committed Dec 6, 2012
1 parent 57cf860 commit d62331e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ gm(readStream, 'img.jpg')

````

## Buffer as source
```` js
var gm = require('../')
, fs = require('fs')

var buf = fs.readFileSync(__dirname + '/image.jpg');

gm(buf)
.noise('laplacian')
.write(__dirname + '/out.jpg', function (err) {
if (err) {
throw err;
}

console.log('Created image from buffer!');
});

````

## Getting started
First download and install [GraphicsMagick](http://www.graphicsmagick.org/)

Expand Down
13 changes: 13 additions & 0 deletions examples/source_buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var gm = require('../')
, fs = require('fs')
, dir = __dirname + '/../examples/imgs'

var buf = fs.readFileSync(dir + '/original.jpg');

gm(buf)
.noise('laplacian')
.write(dir + '/fromBuffer.png', function (err) {
if (err) return console.dir(arguments);

console.log(this.outname + ' created :: ' + arguments[3]);
});
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,26 @@ function gm (source, height, color) {
}
}

// parse out gif frame brackets from filename
// since stream doesn't use source path
// eg. "filename.gif[0]"
var frames = source.match(/(\[.+\])$/);
if (frames) {
this.sourceFrames = source.substr(frames.index, frames[0].length);
source = source.substr(0, frames.index);
if (typeof source === "string") {
// then source is a path

// parse out gif frame brackets from filename
// since stream doesn't use source path
// eg. "filename.gif[0]"
var frames = source.match(/(\[.+\])$/);
if (frames) {
this.sourceFrames = source.substr(frames.index, frames[0].length);
source = source.substr(0, frames.index);
}
}

this.source = source;

this.addSrcFormatter(function (src) {
// must be first source formatter
var ret = this.sourceStream ? '-' : this.source;
var inputFromStdin = this.sourceStream || Buffer.isBuffer(this.source);

var ret = inputFromStdin ? '-' : this.source;
if (ret && this.sourceFrames) ret += this.sourceFrames;
src.length = 0;
src[0] = ret;
Expand Down
5 changes: 5 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ module.exports = function (proto) {

debug(cmd);

if (Buffer.isBuffer(this.source)) {
proc.stdin.write(this.source);
proc.stdin.end();
}

// pipe in the sourceStream if present
if (self.sourceStream) {

Expand Down

0 comments on commit d62331e

Please sign in to comment.