Skip to content

Commit

Permalink
Implement sharp 0.15 resize changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
vseventer committed Jun 21, 2016
1 parent 5d86b19 commit 689b56c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.0 (June 21, 2016)
* Enhancement: add [`--kernel`](http://sharp.dimens.io/en/stable/api/#resizewidth-height-options).
* Enhancement: rename `--interpolateWith` to [`--interpolator`](http://sharp.dimens.io/en/stable/api/#resizewidth-height-options).

## 0.4.2 (June 21, 2016)
* Bugfix: mark `--grayscale` as boolean option.
* Enhancement: update dependencies, including `sharp` (`0.15.x`).
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ $ sharp --help
--grayscale, --greyscale Convert to grayscale.
-h, --height <number> Scale output to height.
--help Output usage information.
--kernel <string> Kernel to use for image reduction (cubic, lanczo2, lanczo3). Defaults
to lanczo3. Use in conjunction with --width or --height.
--ignoreAspectRatio Stretch the image to the exact width and/or height specified.
--interpolateWith <string> Use the specified interpolator (nearest, bilinear, bicubic,
--interpolator <string> Use the specified interpolator (nearest, bilinear, bicubic,
vertexSplitQuadraticBasisSpline (vsqbs), locallyBoundedBicubic (lbb),
nohalo) for image resizing.
nohalo) for image resizing. Use in conjunction with --width or
--height.
--limitInputPixels <number> Do not process input images where the number of pixels
(width × height) exceeds the specified amount.
--max Resize the image to be as large as possible while ensuring its
Expand Down
7 changes: 5 additions & 2 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ Options
--grayscale, --greyscale Convert to grayscale.
-h, --height <number> Scale output to height.
--help Output usage information.
--kernel <string> Kernel to use for image reduction (cubic, lanczo2, lanczo3). Defaults
to lanczo3. Use in conjunction with --width or --height.
--ignoreAspectRatio Stretch the image to the exact width and/or height specified.
--interpolateWith <string> Use the specified interpolator (nearest, bilinear, bicubic,
--interpolator <string> Use the specified interpolator (nearest, bilinear, bicubic,
vertexSplitQuadraticBasisSpline (vsqbs), locallyBoundedBicubic (lbb),
nohalo) for image resizing.
nohalo) for image resizing. Use in conjunction with --width or
--height.
--limitInputPixels <number> Do not process input images where the number of pixels
(width × height) exceeds the specified amount.
--max Resize the image to be as large as possible while ensuring its
Expand Down
13 changes: 4 additions & 9 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ var run = function(src, flags) {
// Resize options.
// ===============

// @see http://sharp.dimens.io/en/stable/api/#resizewidth-height
// @see http://sharp.dimens.io/en/stable/api/#resizewidth-height-options
if(flags.height || flags.width) {
// NOTE: Sharp deals with NaN when width or height are not specified.
var width = parseInt(flags.width, 10) || null,
height = parseInt(flags.height, 10) || null;
image.resize(width, height);
height = parseInt(flags.height, 10) || null,
opts = { interpolator: flags.interpolator, kernel: flags.kernel };
image.resize(width, height, opts);
}

// @see http://sharp.dimens.io/en/stable/api/#cropgravity
Expand Down Expand Up @@ -95,11 +95,6 @@ var run = function(src, flags) {
image.ignoreAspectRatio();
}

// @see http://sharp.dimens.io/en/stable/api/#interpolatewithinterpolator
if(flags.interpolateWith) {
image.interpolateWith(flags.interpolateWith);
}

// Operations.
// ============

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" : "sharp-cli",
"version" : "0.4.2",
"version" : "0.5.0",
"description" : "CLI for sharp.",
"keywords" : [ "cli", "jpeg", "libvips", "png", "sharp", "vips", "webp" ],
"homepage" : "https://github.com/vseventer/sharp-cli",
Expand Down
Binary file modified test/fixtures/input.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 49 additions & 12 deletions test/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,41 +565,78 @@ describe('runner', function() {
this.flags.height = 100;
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ null, 100 ]);
expect(spy.args[0]).to.eql([ null, 100, { interpolator: undefined, kernel: undefined } ]);
});
});
it('--height <number> (invalid value)', function() {
var spy = this.spyOn('resize');
this.flags.height = 'foo';
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ null, null ]);
expect(spy.args[0]).to.eql([ null, null, { interpolator: undefined, kernel: undefined } ]);
});
});

it('--ignoreAspectRatio', testBoolean('ignoreAspectRatio'));

it('--interpolateWith <string>', function() {
var spy = this.spyOn('interpolateWith');
this.flags.interpolateWith = 'bilinear';
it('--interpolator <string>', function() {
var spy = this.spyOn('resize');
this.flags.width = 100;
this.flags.interpolator = 'bilinear';
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ 'bilinear' ]);
expect(spy.args[0]).to.eql([ 100, null, {
interpolator: 'bilinear',
kernel: undefined
} ]);
});
});
it('--interpolateWith <string> (invalid value)', function() {
var spy = this.spyOn('interpolateWith');
this.flags.interpolateWith = 'foo';
it('--interpolator <string> (invalid value)', function() {
var spy = this.spyOn('resize');
this.flags.width = 100;
this.flags.interpolator = 'foo';
return runner.run(input, this.flags).then(function() {
throw new Error('TRIGGER REJECTION');
}).catch(function(err) {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ 'foo' ]);
expect(spy.args[0]).to.eql([ 100, null, {
interpolator: 'foo',
kernel: undefined
} ]);
expect(err).to.have.property('message');
expect(err.message.toLowerCase()).to.contain('invalid interpolator');
});
});

it('--kernel <string>', function() {
var spy = this.spyOn('resize');
this.flags.width = 100;
this.flags.kernel = 'cubic';
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ 100, null, {
interpolator: undefined,
kernel: 'cubic'
} ]);
});
});
it('--kernel <string> (invalid value)', function() {
var spy = this.spyOn('resize');
this.flags.width = 100;
this.flags.kernel = 'foo';
return runner.run(input, this.flags).then(function() {
throw new Error('TRIGGER REJECTION');
}).catch(function(err) {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ 100, null, {
interpolator: undefined,
kernel: 'foo'
} ]);
expect(err).to.have.property('message');
expect(err.message.toLowerCase()).to.contain('invalid kernel');
});
});

it('--limitInputPixels <number>', function() {
var spy = this.spyOn('limitInputPixels');
this.flags.limitInputPixels = '100000';
Expand Down Expand Up @@ -941,15 +978,15 @@ describe('runner', function() {
this.flags.width = 100;
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ 100, null ]);
expect(spy.args[0]).to.eql([ 100, null, { interpolator: undefined, kernel: undefined } ]);
});
});
it('--width <number> (invalid value)', function() {
var spy = this.spyOn('resize');
this.flags.width = 'foo';
return runner.run(input, this.flags).then(function() {
expect(spy).to.be.calledOnce;
expect(spy.args[0]).to.eql([ null, null ]);
expect(spy.args[0]).to.eql([ null, null, { interpolator: undefined, kernel: undefined } ]);
});
});

Expand Down

0 comments on commit 689b56c

Please sign in to comment.