diff --git a/docs/changelog.md b/docs/changelog.md index 5712d2d7..7ef67875 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -14,6 +14,10 @@ Requires libvips v8.6.1. [#1121](https://github.com/lovell/sharp/pull/1121) [@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 - 11th January 2018 * Expose offset coordinates of strategy-based crop. diff --git a/src/pipeline.cc b/src/pipeline.cc index bd2cf6d5..49b5ecaa 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -465,7 +465,10 @@ class PipelineWorker : public Nan::AsyncWorker { ->set("extend", VIPS_EXTEND_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 if (baton->crop < 9) { // Gravity-based crop diff --git a/test/unit/crop.js b/test/unit/crop.js index 67c2d7bc..0f9f5a61 100644 --- a/test/unit/crop.js +++ b/test/unit/crop.js @@ -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 () { it('JPEG', function (done) { sharp(fixtures.inputJpg)