-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Images] Images Bindings #20141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Images] Images Bindings #20141
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| pcx_content_type: how-to | ||
| title: Bind to Workers API | ||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| 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/). | ||
|
|
||
| You can bind the Images API to your Worker to transform, resize, and encode images without requiring them to be accessible through a URL. | ||
|
|
||
| For example, when you allow Workers to interact with Images, you can: | ||
|
|
||
| - Transform an image, then upload the output image directly into R2 without serving to the browser. | ||
| - Optimize an image stored in R2 by passing the blob of bytes representing the image, instead of fetching the public URL for the image. | ||
| - Resize an image, overlay the output over a second image as a watermark, then resize this output into a final result. | ||
|
|
||
| Bindings can be configured in the Cloudflare dashboard for your Worker or in the `wrangler.toml` file in your project's directory. | ||
|
|
||
| ## Setup | ||
|
|
||
| The Images binding is enabled on a per-Worker basis. | ||
|
|
||
| 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. | ||
|
|
||
| To bind Images to your Worker, add the following to the end of your `wrangler.toml` file: | ||
|
|
||
|
|
||
| ```txt | ||
| [images] | ||
| binding = "IMAGES" # i.e. available in your Worker on env.IMAGES | ||
| ``` | ||
|
|
||
| Within your Worker code, you can interact with this binding by using `env.IMAGES`. | ||
|
|
||
| ## Methods | ||
|
|
||
|
|
||
| ### `.transform()` | ||
|
|
||
| - 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`. | ||
|
|
||
| ### `.draw()` | ||
|
|
||
| - Allows [drawing an image](/images/transform-images/draw-overlays/) over another image. | ||
| - 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. | ||
|
|
||
|
|
||
| ### `.output()` | ||
|
|
||
| * Defines the [output format](/images/transform-images/) for the transformed image such as AVIF, WebP, and JPEG. | ||
|
|
||
| For example, to rotate, resize, and blur an image, then output the image as AVIF: | ||
|
|
||
| ```js | ||
| const info = await env.IMAGES.info(stream); | ||
| // stream contains a valid image, and width/height is available on the info object | ||
|
|
||
| const response = ( | ||
| await env.IMAGES.input(stream) | ||
| .transform({ rotate: 90 }) | ||
| .transform({ width: 128 }) | ||
| .output({ format: "image/avif" }) | ||
| ).response(); | ||
|
|
||
| return response; | ||
| ``` | ||
|
|
||
| In this example, the transformed image is outputted as a WebP. | ||
|
|
||
| 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. | ||
|
|
||
| ## Interact with your Images binding locally | ||
|
|
||
| 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. | ||
|
|
||
| Wrangler supports two different versions of the Images API: | ||
|
|
||
| - 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. | ||
| - A low-fidelity version that supports only a subset of features, such as resizing and rotation. | ||
|
|
||
| To test the high-fidelity version of Images, you can run `wrangler dev`: | ||
|
|
||
| ```txt | ||
| npx wrangler dev | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| To test the low-fidelity version of Images, add the `--experimental-images-local-mode` flag: | ||
|
|
||
| ```txt | ||
| npm wrangler dev --experimental-images-local-mode | ||
| ``` | ||
|
|
||
| Currently, this version supports only `width`, `height`, `rotate`, and `format`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: Images | ||
| external_link: /images | ||
| head: [] | ||
| description: Store, transform, optimize, and deliver images at scale. | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.