Skip to content

Commit b1d1d54

Browse files
authored
[Images] Images Bindings (#20141)
* Images updates for bindings * Added Images Binding to Workers * Fixed relative links * Small edits and added content on caching * Update code example
1 parent 29c62a9 commit b1d1d54

File tree

5 files changed

+156
-1
lines changed

5 files changed

+156
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Bind to Workers API
4+
sidebar:
5+
order: 4
6+
---
7+
8+
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+
10+
You can bind the Images API to your Worker to transform, resize, and encode images without requiring them to be accessible through a URL.
11+
12+
For example, when you allow Workers to interact with Images, you can:
13+
14+
- Transform an image, then upload the output image directly into R2 without serving to the browser.
15+
- Optimize an image stored in R2 by passing the blob of bytes representing the image, instead of fetching the public URL for the image.
16+
- Resize an image, overlay the output over a second image as a watermark, then resize this output into a final result.
17+
18+
Bindings can be configured in the Cloudflare dashboard for your Worker or in the `wrangler.toml` file in your project's directory.
19+
20+
## Setup
21+
22+
The Images binding is enabled on a per-Worker basis.
23+
24+
You can define variables in the `wrangler.toml` file of your Worker project's directory. These variables are bound to external resources at runtime, and you can then interact with them through this variable.
25+
26+
To bind Images to your Worker, add the following to the end of your `wrangler.toml` file:
27+
28+
29+
```txt
30+
[images]
31+
binding = "IMAGES" # i.e. available in your Worker on env.IMAGES
32+
```
33+
34+
Within your Worker code, you can interact with this binding by using `env.IMAGES`.
35+
36+
## Methods
37+
38+
39+
### `.transform()`
40+
41+
- Defines how an image should be optimized and manipulated through [`fetch()` options](/images/transform-images/transform-via-workers/#fetch-options) such as `width`, `height`, and `blur`.
42+
43+
### `.draw()`
44+
45+
- Allows [drawing an image](/images/transform-images/draw-overlays/) over another image.
46+
- The overlaid image can be manipulated using `opacity`, `repeat`, `top`, `left`, `bottom`, and `right`. To apply other `fetch()` options, you can pass a child `.transform()` function inside this method.
47+
48+
49+
### `.output()`
50+
51+
* Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG.
52+
53+
For example, to rotate, resize, and blur an image, then output the image as AVIF:
54+
55+
```js
56+
​​const info = await env.IMAGES.info(stream);
57+
// stream contains a valid image, and width/height is available on the info object
58+
59+
const response = (
60+
await env.IMAGES.input(stream)
61+
.transform({ rotate: 90 })
62+
.transform({ width: 128 })
63+
.output({ format: "image/avif" })
64+
).response();
65+
66+
return response;
67+
```
68+
69+
In this example, the transformed image is outputted as a WebP.
70+
71+
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.
72+
73+
## Interact with your Images binding locally
74+
75+
The Images API can be used in local development through [Wrangler](/workers/wrangler/install-and-update/), the command-line interface for Workers. Using the Images binding in local development will not incur usage charges.
76+
77+
Wrangler supports two different versions of the Images API:
78+
79+
- 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.
80+
- A low-fidelity version that supports only a subset of features, such as resizing and rotation.
81+
82+
To test the high-fidelity version of Images, you can run `wrangler dev`:
83+
84+
```txt
85+
npx wrangler dev
86+
```
87+
88+
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.
89+
90+
To test the low-fidelity version of Images, add the `--experimental-images-local-mode` flag:
91+
92+
```txt
93+
npm wrangler dev --experimental-images-local-mode
94+
```
95+
96+
Currently, this version supports only `width`, `height`, `rotate`, and `format`.

src/content/docs/images/transform-images/draw-overlays.mdx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: reference
33
title: Draw overlays and watermarks
44
sidebar:
5-
order: 3
5+
order: 5
66

77
---
88

@@ -68,6 +68,39 @@ The `draw` property is an array. Overlays are drawn in the order they appear in
6868
* `rotate`
6969
* Number of degrees to rotate the overlay image by. Same as [for the main image](/images/transform-images/transform-via-workers/#fetch-options).
7070

71+
## Draw using the Images binding
72+
73+
When [interacting with Images through a binding](/images/transform-images/bindings/), the Images API supports a `.draw()` method.
74+
75+
The accepted options for the overlaid image are `opacity`, `repeat`, `top`, `left`, `bottom`, and `right`.
76+
77+
```js
78+
// Fetch image and watermark
79+
const img = await fetch('https://example.com/image.png');
80+
const watermark = await fetch('https://example.com/watermark.png');
81+
82+
const response = await env.IMAGES.input(img.body)
83+
.transform({ width: 1024 })
84+
.draw(watermark.body, { "opacity": 0.25, "repeat": true })
85+
.output({ format: "image/avif" })
86+
.response();
87+
88+
return response;
89+
```
90+
91+
To apply [other `fetch()` options](/images/transform-images/transform-via-workers/) to the overlaid image, you can pass a child `.transform()` function inside the `.draw()` request.
92+
93+
In the example below, the watermark is manipulated with `rotate` and `width` before being drawn over the base image with the `opacity` and `rotate` options.
94+
95+
```js
96+
// Fetch image and watermark
97+
const response = (
98+
await env.IMAGES.input(img.body)
99+
.transform({ width: 1024 })
100+
.draw(watermark.body, { "opacity": 0.25, "repeat": true })
101+
.output({ format: "image/avif" })
102+
).response();
103+
```
71104

72105
## Examples
73106

src/content/docs/workers/local-development.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ npx wrangler dev
4141
| Durable Objects |||
4242
| Email Bindings |||
4343
| Hyperdrive |||
44+
| Images |||
4445
| KV |||
4546
| mTLS |||
4647
| Queues |||
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Images
4+
external_link: /images
5+
head: []
6+
description: Store, transform, optimize, and deliver images at scale.
7+
8+
---

src/content/docs/workers/wrangler/configuration.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,23 @@ id = "<ID>"
658658

659659
</WranglerConfig>
660660

661+
### Images
662+
663+
[Cloudflare Images](/images/transform-images/transform-via-workers/) lets you make transformation requests to optimize, resize, and manipulate images stored in remote sources.
664+
665+
To bind Images to your Worker, assign an array of the below object to the `images` key.
666+
667+
`binding` (required). The name of the binding used to refer to the Images API.
668+
669+
<WranglerConfig>
670+
671+
```toml title="wrangler.toml"
672+
[[images]]
673+
binding = "IMAGES" # i.e. available in your Worker on env.IMAGES
674+
```
675+
676+
</WranglerConfig>
677+
661678
### KV namespaces
662679

663680
[Workers KV](/kv/api/) is a global, low-latency, key-value data store. It stores data in a small number of centralized data centers, then caches that data in Cloudflare’s data centers after access.

0 commit comments

Comments
 (0)