Skip to content

Commit 5ab6f59

Browse files
committed
Prevent crop when at or below target dimensions lovell#1134
1 parent 8b80626 commit 5ab6f59

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Requires libvips v8.6.1.
1414
[#1121](https://github.com/lovell/sharp/pull/1121)
1515
[@BiancoA](https://github.com/BiancoA)
1616

17+
* Prevent crop operation when image already at or below target dimensions.
18+
[#1134](https://github.com/lovell/sharp/issues/1134)
19+
[@pieh](https://github.com/pieh)
20+
1721
#### v0.19.0 - 11<sup>th</sup> January 2018
1822

1923
* Expose offset coordinates of strategy-based crop.

src/pipeline.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,10 @@ class PipelineWorker : public Nan::AsyncWorker {
465465
->set("extend", VIPS_EXTEND_BACKGROUND)
466466
->set("background", background));
467467

468-
} else if (baton->canvas != Canvas::IGNORE_ASPECT) {
468+
} else if (
469+
baton->canvas != Canvas::IGNORE_ASPECT &&
470+
(image.width() > baton->width || image.height() > baton->height)
471+
) {
469472
// Crop/max/min
470473
if (baton->crop < 9) {
471474
// Gravity-based crop

test/unit/crop.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ describe('Crop', function () {
159159
});
160160
});
161161

162+
it('Skip crop when post-resize dimensions are at or below target dimensions', function () {
163+
return sharp(fixtures.inputJpg)
164+
.resize(1600, 1200)
165+
.toBuffer()
166+
.then(function (input) {
167+
return sharp(input)
168+
.resize(1110)
169+
.crop(sharp.strategy.attention)
170+
.toBuffer({ resolveWithObject: true })
171+
.then(function (result) {
172+
assert.strictEqual(1110, result.info.width);
173+
assert.strictEqual(832, result.info.height);
174+
assert.strictEqual(undefined, result.info.cropOffsetLeft);
175+
assert.strictEqual(undefined, result.info.cropOffsetTop);
176+
});
177+
});
178+
});
179+
162180
describe('Entropy-based strategy', function () {
163181
it('JPEG', function (done) {
164182
sharp(fixtures.inputJpg)

0 commit comments

Comments
 (0)