Skip to content

Commit

Permalink
Add support for passing the crop strategy as a string (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou authored and lovell committed Mar 16, 2017
1 parent 6b1d698 commit 9707f8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ const crop = function crop (crop) {
} else if (is.integer(crop) && crop >= strategy.entropy) {
// Strategy
this.options.crop = crop;
} else if (is.string(crop) && is.integer(strategy[crop])) {
// Strategy (string)
this.options.crop = strategy[crop];
} else {
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
}
Expand Down
32 changes: 32 additions & 0 deletions test/unit/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ describe('Crop', function () {
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});

it('supports the strategy passed as a string', function (done) {
sharp(fixtures.inputPngWithTransparency)
.resize(320, 80)
.crop('entropy')
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels);
assert.strictEqual(320, info.width);
assert.strictEqual(80, info.height);
assert.strictEqual(0, info.cropCalcLeft);
assert.strictEqual(80, info.cropCalcTop);
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});
});

describe('Attention strategy', function () {
Expand Down Expand Up @@ -225,5 +241,21 @@ describe('Crop', function () {
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});

it('supports the strategy passed as a string', function (done) {
sharp(fixtures.inputPngWithTransparency)
.resize(320, 80)
.crop('attention')
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels);
assert.strictEqual(320, info.width);
assert.strictEqual(80, info.height);
assert.strictEqual(0, info.cropCalcLeft);
assert.strictEqual(80, info.cropCalcTop);
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});
});
});

0 comments on commit 9707f8c

Please sign in to comment.