diff --git a/README.md b/README.md index bb6be3b1..a30cb41d 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ gm('/path/to/my/img.jpg') ```js var fs = require('fs') - , gm = require('gm'); + , gm = require('gm') + , im = require('gm').subClass({ imageMagick: true }); // resize and remove EXIF profile data gm('/path/to/my/img.jpg') @@ -126,6 +127,24 @@ gm(200, 400, "#ddff99f3") .write("/path/to/brandNewImg.jpg", function (err) { // ... }); + +// also supports imagemagick stacks +im('test.psd[0]') +.rotate('green', '-90') // rotates only once for both outputs +.pipeline() + .clone(0) + .autoOrient() + .resize(100, 100) + .write('test100.png') +.close() +.pipeline() + .clone(0) + .resize(500, 500) + .write('test500.jpg') +.close() +.write("NULL:", function (err) { // throw away the collection of layers + // ... +}); ``` ## Streams diff --git a/index.js b/index.js index 6e622d8a..738da649 100644 --- a/index.js +++ b/index.js @@ -118,6 +118,7 @@ require("./lib/command")(gm.prototype); require("./lib/compare")(gm.prototype); require("./lib/composite")(gm.prototype); require("./lib/montage")(gm.prototype); +require("./lib/pipeline")(gm.prototype); /** * Expose. diff --git a/lib/pipeline.js b/lib/pipeline.js new file mode 100644 index 00000000..a67ed177 --- /dev/null +++ b/lib/pipeline.js @@ -0,0 +1,39 @@ + +module.exports = function (proto) { + proto.pipeline = function () { + if (!this._options.imageMagick) { + throw new Error('Batching is not supported by GraphicsMagick'); + } + + var gm = new IMPipeline(this, arguments); + gm.options(this._options); + return gm; + }; + + function IMPipeline(parent, args) { + proto.constructor.apply(this, args); + this._pipeline = parent; + } + IMPipeline.prototype = Object.create(proto); + + IMPipeline.prototype.write = function (name) { + this.out('-write', name); + return this; + } + + IMPipeline.prototype.clone = function (idx) { + if (idx !== undefined) { + this.in('-clone', idx.toString()); + } else { + this.in('+clone'); + } + return this; + }; + + IMPipeline.prototype.close = function () { + this._pipeline.out('('); + this._pipeline.out.apply(this._pipeline, this.args().slice(1, -1)); + this._pipeline.out(')'); + return this._pipeline; + }; +};