Skip to content

Commit a333b87

Browse files
authored
Prevent upsampling via libwebp (#3267)
1 parent 4662527 commit a333b87

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/pipeline.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ class PipelineWorker : public Napi::AsyncWorker {
188188
if (jpegShrinkOnLoad > 1 && static_cast<int>(shrink) == jpegShrinkOnLoad) {
189189
jpegShrinkOnLoad /= 2;
190190
}
191-
} else if (inputImageType == sharp::ImageType::WEBP ||
192-
inputImageType == sharp::ImageType::SVG ||
191+
} else if (inputImageType == sharp::ImageType::WEBP && shrink > 1.0) {
192+
// Avoid upscaling via webp
193+
scale = 1.0 / shrink;
194+
} else if (inputImageType == sharp::ImageType::SVG ||
193195
inputImageType == sharp::ImageType::PDF) {
194196
scale = 1.0 / shrink;
195197
}

test/unit/resize.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ describe('Resize dimensions', function () {
121121
});
122122
});
123123

124+
it('Webp resize then extract large image', function (done) {
125+
sharp(fixtures.inputWebP)
126+
.resize(0x4000, 0x4000)
127+
.extract({ top: 0x2000, left: 0x2000, width: 256, height: 256 })
128+
.webp()
129+
.toBuffer(function (err, data, info) {
130+
if (err) throw err;
131+
assert.strictEqual('webp', info.format);
132+
assert.strictEqual(256, info.width);
133+
assert.strictEqual(256, info.height);
134+
done();
135+
});
136+
});
137+
124138
it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (done) {
125139
sharp(fixtures.inputJpg)
126140
.resize(1080, 607)

0 commit comments

Comments
 (0)