From 9707f8c5d2055931b393a7a21f670fbe96a3e7bd Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 16 Mar 2017 15:27:09 +0100 Subject: [PATCH] Add support for passing the crop strategy as a string (#735) --- lib/resize.js | 3 +++ test/unit/crop.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/resize.js b/lib/resize.js index d77add634..b5337d302 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -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); } diff --git a/test/unit/crop.js b/test/unit/crop.js index cb6333e18..b8087a3ab 100644 --- a/test/unit/crop.js +++ b/test/unit/crop.js @@ -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 () { @@ -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); + }); + }); }); });