From a333b87f5ddab03dc669729af9f7744809e980e7 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 20 Jun 2022 21:49:53 +1200 Subject: [PATCH] Prevent upsampling via libwebp (#3267) --- src/pipeline.cc | 6 ++++-- test/unit/resize.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/pipeline.cc b/src/pipeline.cc index 2744d3d26..65f994b07 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -188,8 +188,10 @@ class PipelineWorker : public Napi::AsyncWorker { if (jpegShrinkOnLoad > 1 && static_cast(shrink) == jpegShrinkOnLoad) { jpegShrinkOnLoad /= 2; } - } else if (inputImageType == sharp::ImageType::WEBP || - inputImageType == sharp::ImageType::SVG || + } else if (inputImageType == sharp::ImageType::WEBP && shrink > 1.0) { + // Avoid upscaling via webp + scale = 1.0 / shrink; + } else if (inputImageType == sharp::ImageType::SVG || inputImageType == sharp::ImageType::PDF) { scale = 1.0 / shrink; } diff --git a/test/unit/resize.js b/test/unit/resize.js index b01daa4cf..3e29f3a73 100644 --- a/test/unit/resize.js +++ b/test/unit/resize.js @@ -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) { sharp(fixtures.inputJpg) .resize(1080, 607)