|
| 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`. |
0 commit comments