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/changelog/images/2025-02-21-images-bindings-in-workers.mdx
+18-10Lines changed: 18 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,32 +5,40 @@ description: >
5
5
date: 2025-02-24T12:00:00Z
6
6
---
7
7
8
+
import { WranglerConfig } from"~/components";
9
+
8
10
You can now [interact with the Images API](/images/transform-images/bindings/) directly in your Worker.
9
11
10
12
This allows more fine-grained control over transformation request flows and cache behavior. For example, you can resize, manipulate, and overlay images without requiring them to be accessible through a URL.
11
13
12
14
The Images binding can be configured in the Cloudflare dashboard for your Worker or in the `wrangler.toml` file in your project's directory:
13
15
14
-
```toml
15
-
[images]
16
-
binding = "IMAGES"# i.e. available in your Worker on env.IMAGES
16
+
<WranglerConfig>
17
+
18
+
```jsonc
19
+
{
20
+
"images": {
21
+
"binding":"IMAGES", // i.e. available in your Worker on env.IMAGES
22
+
},
23
+
}
17
24
```
18
25
26
+
</WranglerConfig>
27
+
19
28
Within your Worker code, you can interact with this binding by using `env.IMAGES`.
20
29
21
30
Here's how you can rotate, resize, and blur an image, then output the image as AVIF:
22
31
23
32
```ts
24
-
const info =awaitenv.IMAGES.info(stream);
33
+
const info =awaitenv.IMAGES.info(stream);
25
34
// stream contains a valid image, and width/height is available on the info object
Copy file name to clipboardExpand all lines: src/content/docs/images/transform-images/bindings.mdx
+20-12Lines changed: 20 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,8 @@ sidebar:
5
5
order: 4
6
6
---
7
7
8
+
import { WranglerConfig } from"~/components";
9
+
8
10
A [binding](/workers/runtime-apis/bindings/) connects your [Worker](/workers/) to external resources on the Developer Platform, like [Images](/images/transform-images/transform-via-workers/), [R2 buckets](/r2/buckets/), or [KV Namespaces](/kv/concepts/kv-namespaces/).
9
11
10
12
You can bind the Images API to your Worker to transform, resize, and encode images without requiring them to be accessible through a URL.
@@ -25,12 +27,18 @@ You can define variables in the `wrangler.toml` file of your Worker project's di
25
27
26
28
To bind Images to your Worker, add the following to the end of your `wrangler.toml` file:
27
29
30
+
<WranglerConfig>
28
31
29
-
```txt
30
-
[images]
31
-
binding = "IMAGES" # i.e. available in your Worker on env.IMAGES
32
+
```jsonc
33
+
{
34
+
"images": {
35
+
"binding":"IMAGES", // i.e. available in your Worker on env.IMAGES
36
+
},
37
+
}
32
38
```
33
39
40
+
</WranglerConfig>
41
+
34
42
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
43
36
44
## Methods
@@ -69,28 +77,28 @@ return response;
69
77
70
78
### `.output()`
71
79
72
-
* Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG.
80
+
- Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG.
73
81
74
82
For example, to rotate, resize, and blur an image, then output the image as AVIF:
75
83
76
84
```ts
77
85
const info =awaitenv.IMAGES.info(stream);
78
86
// stream contains a valid image, and width/height is available on the info object
79
-
87
+
80
88
const response = (
81
-
awaitenv.IMAGES.input(stream)
82
-
.transform({ rotate: 90 })
83
-
.transform({ width: 128 })
84
-
.transform({ blur: 20 })
85
-
.output({ format: "image/avif" })
89
+
awaitenv.IMAGES.input(stream)
90
+
.transform({ rotate: 90 })
91
+
.transform({ width: 128 })
92
+
.transform({ blur: 20 })
93
+
.output({ format: "image/avif" })
86
94
).response();
87
-
95
+
88
96
returnresponse;
89
97
```
90
98
91
99
### `.info()`
92
100
93
-
- Outputs information about the image, such as `format`, `fileSize`, `width`, and `height`.
101
+
- Outputs information about the image, such as `format`, `fileSize`, `width`, and `height`.
94
102
95
103
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.
0 commit comments