Skip to content

Commit

Permalink
Merge pull request #351 from joelmukuthu/master
Browse files Browse the repository at this point in the history
Fix: default crop gravity to sharp.gravity.center
  • Loading branch information
lovell committed Feb 4, 2016
2 parents 322aa60 + da74cd0 commit 320a746
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
21 changes: 21 additions & 0 deletions test/unit/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 320a746

Please sign in to comment.