Skip to content

Commit f98dc3a

Browse files
committed
Add smart_subsample parameter to webp_options. Closes #3
1 parent df3940d commit f98dc3a

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

.changeset/pink-tigers-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@imgproxy/imgproxy-js-core": minor
3+
---
4+
5+
Add `smart_subsample` to `webp_options`

src/options/webpOptions.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { WebpOptions, WebpOptionsPartial } from "../types/webpOptions";
2-
import { guardIsUndef, guardIsValidVal } from "../utils";
2+
import { guardIsNotBool, guardIsUndef, guardIsValidVal } from "../utils";
33

44
const correctOptions = {
55
lossy: true,
@@ -14,11 +14,24 @@ const test = (options: WebpOptionsPartial): boolean => Boolean(getOpt(options));
1414

1515
const build = (options: WebpOptionsPartial): string => {
1616
const webpOptions = getOpt(options);
17+
let compression: string, smart_subsample: boolean | undefined;
1718

1819
guardIsUndef(webpOptions, "webp_options");
19-
guardIsValidVal(correctOptions, webpOptions, "webp_options");
2020

21-
return `webpo:${webpOptions}`;
21+
if (typeof webpOptions === "string") {
22+
compression = webpOptions;
23+
} else {
24+
compression = webpOptions.compression;
25+
26+
if (webpOptions.smart_subsample !== undefined) {
27+
smart_subsample = webpOptions.smart_subsample;
28+
guardIsNotBool(smart_subsample, "webp_options.smart_subsample");
29+
}
30+
}
31+
32+
guardIsValidVal(correctOptions, compression, "webp_options");
33+
34+
return `webpo:${compression}${smart_subsample !== undefined ? `:${smart_subsample}` : ""}`;
2235
};
2336

2437
export { test, build };

src/types/webpOptions.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
/**
2-
* *WEBP options*. **PRO feature**
3-
*
4-
* Allows redefining WebP saving options.
5-
*
62
* Available values:
73
* - `"lossy"` - (default) lossy compression. The lossy compression is based on VP8 key frame encoding.
84
* VP8 is a video compression format created by On2 Technologies as a successor to the VP6 and VP7 formats.
@@ -15,11 +11,27 @@
1511
* For the entropy coding we use a variant of LZ77-Huffman coding, which uses 2D encoding of distance values
1612
* and compact sparse values.
1713
*
14+
*/
15+
type WebPCompressionOptions = "lossy" | "near_lossless" | "lossless";
16+
17+
/**
18+
* *WEBP options*. **PRO feature**
19+
*
20+
* Allows redefining WebP saving options.
21+
*
1822
* @default "lossy"
1923
*
2024
* @see {@link https://docs.imgproxy.net/generating_the_url?id=webp-options | WEBP options imgproxy docs}
2125
*/
22-
type WebpOptions = "lossy" | "near_lossless" | "lossless";
26+
type WebpOptions =
27+
| WebPCompressionOptions
28+
| {
29+
compression: WebPCompressionOptions;
30+
/**
31+
* when `true`, enables smart subsampling. Smart subsampling increases the resulting file size and compression time but improves quality. Default: `false`
32+
*/
33+
smart_subsample?: boolean;
34+
};
2335

2436
/**
2537
* *WEBP options option*. **PRO feature**

tests/optionsBasic/webpOptions.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,21 @@ describe("webpOptions", () => {
4242
it("should return webpo:near_lossless if webpo option is near_lossless", () => {
4343
expect(build({ webpo: "near_lossless" })).toEqual("webpo:near_lossless");
4444
});
45+
46+
it("should support `smart_subsample` option", () => {
47+
expect(build({ webp_options: { compression: "lossy" } })).toEqual(
48+
"webpo:lossy"
49+
);
50+
51+
expect(
52+
build({ webp_options: { compression: "lossy", smart_subsample: true } })
53+
).toEqual("webpo:lossy:true");
54+
55+
expect(
56+
build({
57+
webp_options: { compression: "lossy", smart_subsample: false },
58+
})
59+
).toEqual("webpo:lossy:false");
60+
});
4561
});
4662
});

0 commit comments

Comments
 (0)