Add WebP 'exact' option for control over transparent pixels

This commit is contained in:
Lovell Fuller
2026-01-01 19:19:20 +00:00
parent 1cf4b7f04d
commit 0d872bd13a
9 changed files with 47 additions and 4 deletions

View File

@@ -548,8 +548,8 @@ sharp('input.tiff').jxl({ decodingTier: 4 }).toFile('out.jxl');
sharp('input.tiff').jxl({ lossless: true }).toFile('out.jxl');
sharp('input.tiff').jxl({ effort: 7 }).toFile('out.jxl');
// Support `minSize` and `mixed` webp options
sharp('input.tiff').webp({ minSize: true, mixed: true }).toFile('out.gif');
// Support webp options
sharp('input.tiff').webp({ minSize: true, mixed: true, exact: true }).toFile('out.webp');
// 'failOn' input param
sharp('input.tiff', { failOn: 'none' });

View File

@@ -213,6 +213,33 @@ describe('WebP', () => {
);
});
it('valid exact', () => {
assert.doesNotThrow(() => sharp().webp({ exact: true }));
});
it('invalid exact throws', () => {
assert.throws(
() => sharp().webp({ exact: 'fail' }),
/Expected boolean for webpExact but received fail of type string/
);
});
it('saving exact pixel colour values produces larger file size', async () => {
const withExact = await
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.resize(8, 8)
.webp({ exact: true, effort: 0 })
.toBuffer();
const withoutExact = await
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.resize(8, 8)
.webp({ exact: false, effort: 0 })
.toBuffer()
assert.strictEqual(true, withExact.length > withoutExact.length);
});
it('invalid loop throws', () => {
assert.throws(() => {
sharp().webp({ loop: -1 });