From faa515d9692f542c556d2a47c33df070bb09ac0a Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 19 Aug 2015 14:49:00 +0200 Subject: [PATCH] crop: Permit specifying the gravity as a string. Will be looked up in require('sharp').gravity. --- index.js | 2 ++ test/unit/crop.js | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1259dd38..d86895c7 100755 --- a/index.js +++ b/index.js @@ -143,6 +143,8 @@ Sharp.prototype.crop = function(gravity) { this.options.canvas = 'crop'; if (typeof gravity === 'number' && !Number.isNaN(gravity) && gravity >= 0 && gravity <= 4) { this.options.gravity = gravity; + } else if (typeof gravity === 'string' && typeof module.exports.gravity[gravity] === 'number') { + this.options.gravity = module.exports.gravity[gravity]; } else { throw new Error('Unsupported crop gravity ' + gravity); } diff --git a/test/unit/crop.js b/test/unit/crop.js index d40be672..d74b2190 100755 --- a/test/unit/crop.js +++ b/test/unit/crop.js @@ -81,10 +81,27 @@ describe('Crop gravities', function() { }); }); - it('Invalid', function() { + it('allows specifying the gravity as a string', function(done) { + sharp(fixtures.inputJpg) + .resize(80, 320) + .crop('east') + .toBuffer(function(err, data, info) { + if (err) throw err; + assert.strictEqual(80, info.width); + assert.strictEqual(320, info.height); + fixtures.assertSimilar(fixtures.expected('gravity-east.jpg'), data, done); + }); + }); + + it('Invalid number', function() { assert.throws(function() { sharp(fixtures.inputJpg).crop(5); }); }); + it('Invalid string', function() { + assert.throws(function() { + sharp(fixtures.inputJpg).crop('yadda'); + }); + }); });