mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Add support for passing the crop strategy as a string (#735)
This commit is contained in:
parent
6b1d698448
commit
9707f8c5d2
@ -186,6 +186,9 @@ const crop = function crop (crop) {
|
|||||||
} else if (is.integer(crop) && crop >= strategy.entropy) {
|
} else if (is.integer(crop) && crop >= strategy.entropy) {
|
||||||
// Strategy
|
// Strategy
|
||||||
this.options.crop = crop;
|
this.options.crop = crop;
|
||||||
|
} else if (is.string(crop) && is.integer(strategy[crop])) {
|
||||||
|
// Strategy (string)
|
||||||
|
this.options.crop = strategy[crop];
|
||||||
} else {
|
} else {
|
||||||
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
|
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
|
||||||
}
|
}
|
||||||
|
@ -191,6 +191,22 @@ describe('Crop', function () {
|
|||||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
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 () {
|
describe('Attention strategy', function () {
|
||||||
@ -225,5 +241,21 @@ describe('Crop', function () {
|
|||||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user