Skip to content

Commit c3dd3de

Browse files
authored
feat: Image Optimizer support (#1224)
1 parent 11273f0 commit c3dd3de

40 files changed

+3689
-72
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defaults:
1111
shell: bash
1212
env:
1313
# Note: when updated, also update version in ensure-cargo-installs
14-
viceroy_version: 0.12.2
14+
viceroy_version: 0.15.0
1515
# Note: when updated, also update version in ensure-cargo-installs ! AND ! release-please.yml
1616
wasm-tools_version: 1.216.0
1717
fastly-cli_version: 10.19.0
@@ -46,7 +46,7 @@ jobs:
4646
matrix:
4747
include:
4848
- crate: viceroy
49-
version: 0.12.2 # Note: workflow-level env vars can't be used in matrix definitions
49+
version: 0.15.0 # Note: workflow-level env vars can't be used in matrix definitions
5050
options: ""
5151
- crate: wasm-tools
5252
version: 1.216.0 # Note: workflow-level env vars can't be used in matrix definitions

documentation/docs/globals/fetch.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fetch(resource, options)
7676
- *Fastly-specific*
7777
- `cacheOverride` _**Fastly-specific**_
7878
- `cacheKey` _**Fastly-specific**_
79+
- `imageOptimizerOptions` _**Fastly-specific**_, see [`imageOptimizerOptions`](../../fastly:image-optimizer/imageOptimizerOptions.mdx).
7980
- `fastly` _**Fastly-specific**_
8081
- `decompressGzip`_: boolean_ _**optional**_
8182
- Whether to automatically gzip decompress the Response or not.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Auto`
9+
10+
Enumerator options for [`imageOptimizerOptions.auto`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `AVIF` (`"avif"`) If the browser's Accept header indicates compatibility, deliver an AVIF image.
15+
- `WEBP` (`"webp"`) If the browser's Accept header indicates compatibility, deliver a WebP image.
16+
17+
## Examples
18+
19+
```js
20+
import { Auto, Region } from 'fastly:image-optimizer';
21+
22+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
23+
24+
async function handleRequest(event) {
25+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
26+
imageOptimizerOptions: {
27+
region: Region.UsEast,
28+
auto: Auto.AVIF
29+
},
30+
backend: 'w3'
31+
});
32+
}
33+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `BWAlgorithm`
9+
10+
Enumerator options for [`imageOptimizerOptions.bw`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Threshold` (`"threshold"`) Uses a luminance threshold to convert the image to black and white.
15+
- `Atkinson` (`"atkinson"`) Uses [Atkinson dithering](https://en.wikipedia.org/wiki/Atkinson_dithering) to convert the image to black and white.
16+
17+
18+
## Examples
19+
20+
```js
21+
import { Region, BWAlgorithm } from 'fastly:image-optimizer';
22+
23+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
24+
25+
async function handleRequest(event) {
26+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
27+
imageOptimizerOptions: {
28+
region: Region.UsEast,
29+
bw: BWAlgorithm.Threshold
30+
},
31+
backend: 'w3'
32+
});
33+
}
34+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `CropMode`
9+
10+
Enumerator options for [`imageOptimizerOptions.crop.mode`](./imageOptimizerOptions.mdx) and `imageOptimizerOptions.precrop.mode`.
11+
12+
## Constants
13+
14+
- `Smart` (`"smart"`) Enables content-aware algorithms to attempt to crop the image to the desired aspect ratio while intelligently focusing on the most important visual content, including the detection of faces.
15+
- `Safe` (`"safe"`) Allow cropping out-of-bounds regions.
16+
17+
## Examples
18+
19+
```js
20+
import { CropMode, Region } from 'fastly:image-optimizer';
21+
22+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
23+
24+
async function handleRequest(event) {
25+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
26+
imageOptimizerOptions: {
27+
region: Region.UsEast,
28+
crop: {
29+
size: { ratio: { width: 4, height: 3 } },
30+
mode: CropMode.Smart,
31+
}
32+
},
33+
backend: 'w3'
34+
});
35+
}
36+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Disable`
9+
10+
Enumerator options for [`imageOptimizerOptions.disable`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Upscale` (`"upscale"`) Prevent images being resized such that the output image's dimensions are larger than the source image.
15+
16+
## Examples
17+
18+
```js
19+
import { Disable, Region } from 'fastly:image-optimizer';
20+
21+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
22+
23+
async function handleRequest(event) {
24+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
25+
imageOptimizerOptions: {
26+
region: Region.UsEast,
27+
width: 2560,
28+
disable: Disable.Upscale
29+
},
30+
backend: 'w3'
31+
});
32+
}
33+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Enable`
9+
10+
Enumerator options for [`imageOptimizerOptions.enable`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Upscale` (`"upscale"`) Allow images to be resized such that the output image's dimensions are larger than the source image.
15+
16+
## Examples
17+
18+
```js
19+
import { Enable, Region } from 'fastly:image-optimizer';
20+
21+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
22+
23+
async function handleRequest(event) {
24+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
25+
imageOptimizerOptions: {
26+
region: Region.UsEast,
27+
width: 2560,
28+
enable: Enable.Upscale
29+
},
30+
backend: 'w3'
31+
});
32+
}
33+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Fit`
9+
10+
Enumerator options for [`imageOptimizerOptions.fit`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Bounds` (`"bounds"`) Resize the image to fit entirely within the specified region, making one dimension smaller if needed.
15+
- `Cover` (`"cover"`) Resize the image to entirely cover the specified region, making one dimension larger if needed.
16+
- `Crop` (`"crop"`) Resize and crop the image centrally to exactly fit the specified region.
17+
18+
## Examples
19+
20+
```js
21+
import { Fit, Region } from 'fastly:image-optimizer';
22+
23+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
24+
25+
async function handleRequest(event) {
26+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
27+
imageOptimizerOptions: {
28+
region: Region.UsEast,
29+
width: 150,
30+
height; 150,
31+
fit: Fit.Bounds
32+
},
33+
backend: 'w3'
34+
});
35+
}
36+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Format`
9+
10+
Enumerator options for [`imageOptimizerOptions.format`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Auto` (`"auto"`) Automatically use the best format based on browser support and image/transform characteristics
15+
- `AVIF` (`"avif"`) AVIF
16+
- `BJPG` (`"bjpg"`) Baseline JPEG
17+
- `GIF` (`"gif"`) Graphics Interchange Format
18+
- `JPG` (`"jpg"`) JPEG
19+
- `JXL` (`"jxl"`) JPEGXL
20+
- `MP4` (`"mp4"`) MP4 (H.264)
21+
- `PJPG` (`"pjpg"`) Progressive JPEG
22+
- `PJXL` (`"pjxl"`) Progressive JPEGXL
23+
- `PNG` (`"png"`) Portable Network Graphics
24+
- `PNG8` (`"png8"`) Portable Network Graphics palette image with 256 colors and 8-bit transparency
25+
- `SVG` (`"svg"`) Scalable Vector Graphics
26+
- `WEBP` (`"webp"`) WebP
27+
- `WEBPLL` (`"webpll"`) WebP (Lossless)
28+
- `WEBPLY` (`"webply"`) WebP (Lossy)
29+
30+
## Examples
31+
32+
```js
33+
import { Format, Region } from 'fastly:image-optimizer';
34+
35+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
36+
37+
async function handleRequest(event) {
38+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
39+
imageOptimizerOptions: {
40+
region: Region.UsEast,
41+
format: Format.PNG
42+
},
43+
backend: 'w3'
44+
});
45+
}
46+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# `Metadata`
9+
10+
Enumerator options for [`imageOptimizerOptions.metadata`](./imageOptimizerOptions.mdx).
11+
12+
## Constants
13+
14+
- `Copyright` (`"copyright"`) Preserve [copyright notice](https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata#copyright-notice), creator, credit line, licensor, and web statement of rights fields.
15+
- `C2PA` (`"c2pa"`) Preserve the [C2PA manifest](https://c2pa.org/) and add any transformations performed by Fastly Image Optimizer.
16+
- `CopyRightAndC2PA` (`"copyright,c2pa"`) Resize and crop the image centrally to exactly fit the specified region.
17+
18+
## Examples
19+
20+
```js
21+
import { Metadata, Region } from 'fastly:image-optimizer';
22+
23+
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
24+
25+
async function handleRequest(event) {
26+
return await fetch('https://www.w3.org/Graphics/PNG/text2.png', {
27+
imageOptimizerOptions: {
28+
region: Region.UsEast,
29+
metadata: Metadata.Copyright
30+
},
31+
backend: 'w3'
32+
});
33+
}
34+
```

0 commit comments

Comments
 (0)