You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/images/transform-images/bindings.mdx
+35-14Lines changed: 35 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,38 +31,59 @@ To bind Images to your Worker, add the following to the end of your `wrangler.to
31
31
binding = "IMAGES" # i.e. available in your Worker on env.IMAGES
32
32
```
33
33
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`).
35
35
36
36
## Methods
37
37
38
-
39
38
### `.transform()`
40
39
41
40
- 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`.
42
41
43
42
### `.draw()`
44
43
45
44
- 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.
46
46
- 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.
47
47
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
+
awaitenv.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
+
returnresponse;
68
+
```
48
69
49
70
### `.output()`
50
71
51
72
* Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG.
52
73
53
74
For example, to rotate, resize, and blur an image, then output the image as AVIF:
54
75
55
-
```js
56
-
constinfo=awaitenv.IMAGES.info(stream);
76
+
```ts
77
+
const info =awaitenv.IMAGES.info(stream);
57
78
// stream contains a valid image, and width/height is available on the info object
58
79
59
80
const response = (
60
-
awaitenv.IMAGES.input(stream)
61
-
.transform({ rotate:90 })
62
-
.transform({ width:128 })
63
-
.transform({ blur:20 })
64
-
.output({ format:"image/avif" })
65
-
).response();
81
+
awaitenv.IMAGES.input(stream)
82
+
.transform({ rotate: 90 })
83
+
.transform({ width: 128 })
84
+
.transform({ blur: 20 })
85
+
.output({ format: "image/avif" })
86
+
).response();
66
87
67
88
returnresponse;
68
89
```
@@ -71,8 +92,6 @@ return response;
71
92
72
93
- Outputs information about the image, such as `format`, `fileSize`, `width`, and `height`.
73
94
74
-
In this example, the transformed image is outputted as a WebP.
75
-
76
95
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.
77
96
78
97
## Interact with your Images binding locally
@@ -82,7 +101,7 @@ The Images API can be used in local development through [Wrangler](/workers/wran
82
101
Wrangler supports two different versions of the Images API:
83
102
84
103
- 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.
86
105
87
106
To test the high-fidelity version of Images, you can run `wrangler dev`:
88
107
@@ -92,10 +111,12 @@ npx wrangler dev
92
111
93
112
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.
94
113
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:
96
115
97
116
```txt
98
117
npm wrangler dev --experimental-images-local-mode
99
118
```
100
119
101
120
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