Prevent upsampling via libwebp (#3267)

This commit is contained in:
Blayne Chard 2022-06-20 21:49:53 +12:00 committed by GitHub
parent 4662527a17
commit a333b87f5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -188,8 +188,10 @@ class PipelineWorker : public Napi::AsyncWorker {
if (jpegShrinkOnLoad > 1 && static_cast<int>(shrink) == jpegShrinkOnLoad) { if (jpegShrinkOnLoad > 1 && static_cast<int>(shrink) == jpegShrinkOnLoad) {
jpegShrinkOnLoad /= 2; jpegShrinkOnLoad /= 2;
} }
} else if (inputImageType == sharp::ImageType::WEBP || } else if (inputImageType == sharp::ImageType::WEBP && shrink > 1.0) {
inputImageType == sharp::ImageType::SVG || // Avoid upscaling via webp
scale = 1.0 / shrink;
} else if (inputImageType == sharp::ImageType::SVG ||
inputImageType == sharp::ImageType::PDF) { inputImageType == sharp::ImageType::PDF) {
scale = 1.0 / shrink; scale = 1.0 / shrink;
} }

View File

@ -121,6 +121,20 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Webp resize then extract large image', function (done) {
sharp(fixtures.inputWebP)
.resize(0x4000, 0x4000)
.extract({ top: 0x2000, left: 0x2000, width: 256, height: 256 })
.webp()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('webp', info.format);
assert.strictEqual(256, info.width);
assert.strictEqual(256, info.height);
done();
});
});
it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (done) { it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(1080, 607) .resize(1080, 607)