Skip to content

Commit a7e5c3e

Browse files
committed
Images updates for bindings
1 parent a499554 commit a7e5c3e

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Bind to Workers
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+
## Interact with your Images binding locally
72+
73+
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.
74+
75+
Wrangler supports two different versions of the Images API:
76+
77+
- 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.
78+
- A low-fidelity version that supports only a subset of features, such as resizing and rotation.
79+
80+
To test the high-fidelity version of Images, you can run `wrangler dev`:
81+
82+
```txt
83+
npx wrangler dev
84+
```
85+
86+
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.
87+
88+
To test the low-fidelity version of Images, add the `--experimental-images-local-mode` flag:
89+
90+
```txt
91+
npm wrangler dev --experimental-images-local-mode
92+
```
93+
94+
Currently, this version supports only `width`, `height`, `rotate`, and `format`.

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

Lines changed: 41 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,46 @@ 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 img = await fetch('https://example.com/image.png');
98+
const watermark = await fetch('https://example.com/watermark.png');
99+
100+
const response = await env.IMAGES.input(img.body)
101+
.transform({ width: 1024 })
102+
.draw(env.IMAGES.input(watermark.body).transform({ rotate: 90, width: 128 }), {
103+
opacity: 0.25,
104+
repeat: true,
105+
})
106+
.output({ format: "image/avif" })
107+
.response();
108+
109+
return response;
110+
```
71111

72112
## Examples
73113

0 commit comments

Comments
 (0)