diff --git a/index.js b/index.js index f7179503f..ca0851ed4 100644 --- a/index.js +++ b/index.js @@ -231,7 +231,9 @@ module.exports.gravity = { Sharp.prototype.crop = function(gravity) { this.options.canvas = 'crop'; - if (typeof gravity === 'number' && !Number.isNaN(gravity) && gravity >= 0 && gravity <= 8) { + if (typeof gravity === 'undefined') { + this.options.gravity = module.exports.gravity.center; + } else if (typeof gravity === 'number' && !Number.isNaN(gravity) && gravity >= 0 && gravity <= 8) { this.options.gravity = gravity; } else if (typeof gravity === 'string' && typeof module.exports.gravity[gravity] === 'number') { this.options.gravity = module.exports.gravity[gravity]; diff --git a/test/unit/crop.js b/test/unit/crop.js index 04e72d047..a32281666 100644 --- a/test/unit/crop.js +++ b/test/unit/crop.js @@ -145,4 +145,25 @@ describe('Crop gravities', function() { sharp(fixtures.inputJpg).crop('yadda'); }); }); + + it('does not throw if crop gravity is undefined', function() { + assert.doesNotThrow(function() { + sharp(fixtures.inputJpg).crop(); + }); + }); + + it('defaults crop gravity to sharp.gravity.center', function(done) { + var centerGravitySettings = testSettings.filter(function (settings) { + return settings.name === 'Center'; + })[0]; + sharp(fixtures.inputJpg) + .resize(centerGravitySettings.width, centerGravitySettings.height) + .crop() + .toBuffer(function(err, data, info) { + if (err) throw err; + assert.strictEqual(centerGravitySettings.width, info.width); + assert.strictEqual(centerGravitySettings.height, info.height); + fixtures.assertSimilar(fixtures.expected(centerGravitySettings.fixture), data, done); + }); + }); });