Skip to content

Commit

Permalink
Merge pull request #12 from awenger/master
Browse files Browse the repository at this point in the history
Added Freehand settings
  • Loading branch information
eivindfjeldstad committed Jan 22, 2015
2 parents 1495b0e + eb4ce36 commit 3ba62fd
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 66 deletions.
62 changes: 43 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function ImageMagick (src) {

this.input = '-';
this.output = '-';
this.args = [];
this._operations = [];
this._settings = [];

this.spawn = this.spawn.bind(this);
this.onerror = this.onerror.bind(this);
Expand Down Expand Up @@ -76,7 +77,7 @@ ImageMagick.prototype.outputFormat = function (args) {
*/

ImageMagick.prototype.quality = function (args) {
this.args.push('-quality', args);
this._operations.push('-quality', args);
return this;
};

Expand All @@ -88,7 +89,7 @@ ImageMagick.prototype.quality = function (args) {
*/

ImageMagick.prototype.resize = function (args) {
this.args.push('-resize', args);
this._operations.push('-resize', args);
return this;
};

Expand All @@ -100,7 +101,7 @@ ImageMagick.prototype.resize = function (args) {
*/

ImageMagick.prototype.scale = function (args) {
this.args.push('-scale', args);
this._operations.push('-scale', args);
return this;
};

Expand All @@ -112,7 +113,7 @@ ImageMagick.prototype.scale = function (args) {
*/

ImageMagick.prototype.crop = function (args) {
this.args.push('-crop', args);
this._operations.push('-crop', args);
return this;
};

Expand All @@ -124,7 +125,7 @@ ImageMagick.prototype.crop = function (args) {
*/

ImageMagick.prototype.gravity = function (args) {
this.args.push('-gravity', args);
this._operations.push('-gravity', args);
return this;
};

Expand All @@ -136,7 +137,7 @@ ImageMagick.prototype.gravity = function (args) {
*/

ImageMagick.prototype.thumbnail = function (args) {
this.args.push('-thumbnail', args);
this._operations.push('-thumbnail', args);
return this;
};

Expand All @@ -147,7 +148,7 @@ ImageMagick.prototype.thumbnail = function (args) {
*/

ImageMagick.prototype.autoOrient = function () {
this.args.push('-auto-orient');
this._operations.push('-auto-orient');
return this;
};

Expand All @@ -159,22 +160,39 @@ ImageMagick.prototype.autoOrient = function () {
*/

ImageMagick.prototype.type = function (args) {
this.args.push('-type', args);
this._operations.push('-type', args);
return this;
};

/**
* Passes additional arguments
* Passes additional settings
*
* @param {Object} options
* @api public
*/

ImageMagick.prototype.options = function (options) {
Object.keys(options).forEach(function (key) {
var val = options[key];
this.args.push('-' + key);
if (val != null) this.args.push(val);
ImageMagick.prototype.settings = function(settings) {
Object.keys(settings).forEach(function (key) {
var val = settings[key];
this._settings.push('-' + key);
if (val != null) this._settings.push(val);
}, this);

return this;
}

/**
* Passes additional operations
*
* @param {Object} options
* @api public
*/

ImageMagick.prototype.operations = function (operations) {
Object.keys(operations).forEach(function (key) {
var val = operations[key];
this._operations.push('-' + key);
if (val != null) this._operations.push(val);
}, this);

return this;
Expand Down Expand Up @@ -215,10 +233,8 @@ ImageMagick.prototype.to = function (path) {
*/

ImageMagick.prototype.spawn = function () {
this.args.push(this.output);
this.args.unshift(this.input);

var proc = spawn('convert', this.args);
args = this.args();
var proc = spawn('convert', args);

var stdin = proc.stdin;
stdin.on('error', this.onerror);
Expand All @@ -235,6 +251,14 @@ ImageMagick.prototype.spawn = function () {
this.emit('spawn', proc);
};

/**
* Constructs args for cli call
* @api private
*/
ImageMagick.prototype.args = function() {
return [].concat(this._settings, [this.input], this._operations, [this.output]);
}

/**
* Re-emit errors
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagemagick-stream",
"version": "1.1.0",
"version": "1.2.0",
"description": "Streaming Imagemagick api",
"keywords": [
"stream",
Expand Down
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,29 @@ im('image.png')
.quality(90)
.to('image-resized.png');
```
For freehand arguments, use `.options()`
For freehand operations, use `.operations()`
``` js
im('image.png')
.resize('200x200')
.quality(90)
.options({
.operations({
'strip': undefined,
'gaussian-blur': 0.05,
'interlace': 'Plane'
});
```

For freehand settings, use `.settings()`
``` js
im('image.png')
.resize('200x200')
.quality(90)
.settings({
'density': 400,
'channel': 'RGB'
});
```

## License

MIT
Loading

0 comments on commit 3ba62fd

Please sign in to comment.