Skip to content

Commit fc7d9cc

Browse files
authored
[Images Binding] Document that offline mode is used by default in the Workers vitest integration (#20623)
* Add .draw(..) example, tweak output example * Remove hanging line * Document that offline mode is used for vitest
1 parent 6de3a11 commit fc7d9cc

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

src/content/changelog/images/2025-02-21-images-bindings-in-workers.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ Within your Worker code, you can interact with this binding by using `env.IMAGES
2020

2121
Here's how you can rotate, resize, and blur an image, then output the image as AVIF:
2222

23-
```js
23+
```ts
2424
​​const info = await env.IMAGES.info(stream);
2525
// stream contains a valid image, and width/height is available on the info object
2626

2727
const response = (
28-
await env.IMAGES.input(stream)
29-
.transform({ rotate: 90 })
30-
.transform({ width: 128 })
31-
.output({ format: "image/avif" })
32-
).response();
28+
await env.IMAGES.input(stream)
29+
.transform({ rotate: 90 })
30+
.transform({ width: 128 })
31+
.transform({ blur: 20 })
32+
.output({ format: "image/avif" }
33+
)
34+
).response();
3335

3436
return response;
3537
```

src/content/docs/images/transform-images/bindings.mdx

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,59 @@ To bind Images to your Worker, add the following to the end of your `wrangler.to
3131
binding = "IMAGES" # i.e. available in your Worker on env.IMAGES
3232
```
3333

34-
Within your Worker code, you can interact with this binding by using `env.IMAGES`.
34+
Within your Worker code, you can interact with this binding by using `env.IMAGES.input()` to build an object that can manipulate the image (passed as a `ReadableStream`).
3535

3636
## Methods
3737

38-
3938
### `.transform()`
4039

4140
- Defines how an image should be optimized and manipulated through [parameters](/images/transform-images/transform-via-workers/#fetch-options) such as `width`, `height`, and `blur`.
4241

4342
### `.draw()`
4443

4544
- Allows [drawing an image](/images/transform-images/draw-overlays/) over another image.
45+
- The drawn image can be a stream, or another image returned from `.input()` that has been manipulated.
4646
- The overlaid image can be manipulated using `opacity`, `repeat`, `top`, `left`, `bottom`, and `right`. To apply other parameters, you can pass a child `.transform()` function inside this method.
4747

48+
For example, to draw a resized watermark on an image:
49+
50+
```ts
51+
// Fetch the watermark from Workers Assets, R2, KV etc
52+
const watermark: ReadableStream = ...
53+
54+
// Fetch the main image
55+
const image: ReadableStream = ...
56+
57+
const response = (
58+
await env.IMAGES.input(image)
59+
.draw(
60+
env.IMAGES.input(watermark)
61+
.transform({ width: 32, height: 32}),
62+
{ bottom: 32, right: 32 }
63+
)
64+
.output({ format: "image/avif" })
65+
).response()
66+
67+
return response;
68+
```
4869

4970
### `.output()`
5071

5172
* Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG.
5273

5374
For example, to rotate, resize, and blur an image, then output the image as AVIF:
5475

55-
```js
56-
​​const info = await env.IMAGES.info(stream);
76+
```ts
77+
const info = await env.IMAGES.info(stream);
5778
// stream contains a valid image, and width/height is available on the info object
5879

5980
const response = (
60-
await env.IMAGES.input(stream)
61-
.transform({ rotate: 90 })
62-
.transform({ width: 128 })
63-
.transform({ blur: 20 })
64-
.output({ format: "image/avif" })
65-
).response();
81+
await env.IMAGES.input(stream)
82+
.transform({ rotate: 90 })
83+
.transform({ width: 128 })
84+
.transform({ blur: 20 })
85+
.output({ format: "image/avif" })
86+
).response();
6687

6788
return response;
6889
```
@@ -71,8 +92,6 @@ return response;
7192

7293
- Outputs information about the image, such as `format`, `fileSize`, `width`, and `height`.
7394

74-
In this example, the transformed image is outputted as a WebP.
75-
7695
Responses from the Images binding are not automatically cached. Workers lets you interact directly with the Cache API to customize cache behavior using Workers. You can implement logic in your script to store transformations in Cloudflare’s cache.
7796

7897
## Interact with your Images binding locally
@@ -82,7 +101,7 @@ The Images API can be used in local development through [Wrangler](/workers/wran
82101
Wrangler supports two different versions of the Images API:
83102

84103
- A high-fidelity version that supports all features that are available through the Images API. This is the same version that Cloudflare runs globally in production.
85-
- A low-fidelity version that supports only a subset of features, such as resizing and rotation.
104+
- A low-fidelity offline version that supports only a subset of features, such as resizing and rotation.
86105

87106
To test the high-fidelity version of Images, you can run `wrangler dev`:
88107

@@ -92,10 +111,12 @@ npx wrangler dev
92111

93112
This creates a local-only environment that mirrors the production environment where Cloudflare runs the Images API. You can test your Worker with all available transformation features before deploying to production.
94113

95-
To test the low-fidelity version of Images, add the `--experimental-images-local-mode` flag:
114+
To test the low-fidelity offline version of Images, add the `--experimental-images-local-mode` flag:
96115

97116
```txt
98117
npm wrangler dev --experimental-images-local-mode
99118
```
100119

101120
Currently, this version supports only `width`, `height`, `rotate`, and `format`.
121+
122+
When testing with the [Workers Vitest integration](/workers/testing/vitest-integration/), the low-fidelity offline version is used by default, to avoid hitting the Cloudflare API in tests.

0 commit comments

Comments
 (0)