Prevent crop when at or below target dimensions #1134

This commit is contained in:
Lovell Fuller 2018-02-23 10:31:11 +00:00
parent 8b80626035
commit 5ab6f599fb
3 changed files with 26 additions and 1 deletions

View File

@ -14,6 +14,10 @@ Requires libvips v8.6.1.
[#1121](https://github.com/lovell/sharp/pull/1121) [#1121](https://github.com/lovell/sharp/pull/1121)
[@BiancoA](https://github.com/BiancoA) [@BiancoA](https://github.com/BiancoA)
* Prevent crop operation when image already at or below target dimensions.
[#1134](https://github.com/lovell/sharp/issues/1134)
[@pieh](https://github.com/pieh)
#### v0.19.0 - 11<sup>th</sup> January 2018 #### v0.19.0 - 11<sup>th</sup> January 2018
* Expose offset coordinates of strategy-based crop. * Expose offset coordinates of strategy-based crop.

View File

@ -465,7 +465,10 @@ class PipelineWorker : public Nan::AsyncWorker {
->set("extend", VIPS_EXTEND_BACKGROUND) ->set("extend", VIPS_EXTEND_BACKGROUND)
->set("background", background)); ->set("background", background));
} else if (baton->canvas != Canvas::IGNORE_ASPECT) { } else if (
baton->canvas != Canvas::IGNORE_ASPECT &&
(image.width() > baton->width || image.height() > baton->height)
) {
// Crop/max/min // Crop/max/min
if (baton->crop < 9) { if (baton->crop < 9) {
// Gravity-based crop // Gravity-based crop

View File

@ -159,6 +159,24 @@ describe('Crop', function () {
}); });
}); });
it('Skip crop when post-resize dimensions are at or below target dimensions', function () {
return sharp(fixtures.inputJpg)
.resize(1600, 1200)
.toBuffer()
.then(function (input) {
return sharp(input)
.resize(1110)
.crop(sharp.strategy.attention)
.toBuffer({ resolveWithObject: true })
.then(function (result) {
assert.strictEqual(1110, result.info.width);
assert.strictEqual(832, result.info.height);
assert.strictEqual(undefined, result.info.cropOffsetLeft);
assert.strictEqual(undefined, result.info.cropOffsetTop);
});
});
});
describe('Entropy-based strategy', function () { describe('Entropy-based strategy', function () {
it('JPEG', function (done) { it('JPEG', function (done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)