Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for imagemagick stacks #525

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions lib/pipeline.js
Original file line number Diff line number Diff line change
@@ -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;
};
};