| [ Web Proxy ] |
| Viewing: https://developers.cloudflare.com/images/optimization/binding/ | [Back] [Original] |
A binding connects your Worker to external resources on the Developer Platform, like Images, R2 buckets, or KV namespaces.
The Images binding lets you optimize and manipulate images directly in a Worker. Unlike the URL interface, which requires images to be accessible through a URL, the binding works with raw image bytes. You can pass images from any source, including Images, R2, a fetch() response, or a request body.
With the Images binding, you can:
Bindings can be configured in the Cloudflare dashboard for your Worker or in the Wrangler configuration file in your project's directory.
Billing
Calls to the Images binding are billed as unique transformations: each unique combination of source image and parameters is billed only once per calendar month, and repeat requests within the same month do not incur additional usage. Calls to .info() are free.
Refer to Images pricing for more information about billing.
The Images binding is enabled on a per-Worker basis.
You can define variables in the Wrangler configuration 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 configuration file:
{
"images": {
"binding": "IMAGES", // i.e. available in your Worker on env.IMAGES
},
}[images]
binding = "IMAGES"Within your Worker code, use env.IMAGES.input() to build an object that can manipulate the image (passed as a ReadableStream).
Enable caching
Responses from the Images binding are not automatically cached. Every uncached call performs a full decode and re-encode of the source image, which adds unnecessary latency to every request.
We strongly recommend enabling Workers Cache so that repeat requests are served from cache without re-running your Worker or re-transforming the image. Add the following to your Wrangler configuration file:
{
"cache": {
"enabled": true,
},
}[cache]
enabled = trueThen set Cache-Control headers on your response to control how long transformed images are cached:
const response = (
await env.IMAGES.input(stream)
.transform({ width: 800 })
.output({ format: "image/webp" })
).response();
return new Response(response.body, {
headers: {
...Object.fromEntries(response.headers),
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
},
});const response = (
await env.IMAGES.input(stream)
.transform({ width: 800 })
.output({ format: "image/webp" })
).response();
return new Response(response.body, {
headers: {
...Object.fromEntries(response.headers),
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
},
});Creates an optimization handle for an image. All operations begin with this method. Accepts image bytes up to 20 MB from any source, including Images, R2, a fetch() response, or a request body.
Returns a handle that you can use to chain .transform(), .draw(), and .output() calls.
export default {
async fetch(request, env) {
const imageURL = "https://example.com/photo.jpg";
const response = await fetch(imageURL);
return (
await env.IMAGES.input(response.body)
.transform({ width: 800 })
.output({ format: "image/webp" })
).response();
},
};export default {
async fetch(request, env) {
const imageURL = "https://example.com/photo.jpg";
const response = await fetch(imageURL);
return (
await env.IMAGES
.input(response.body)
.transform({ width: 800 })
.output({ format: "image/webp" })
).response();
},
};Applies optimization parameters to the image, such as width, height, or blur. You can chain multiple .transform() calls to control the order that parameters will be applied.
For the full list of parameters, refer to Features.
The example below shows how you can resize an image that is stored in Images by getting the image bytes:
// Get the raw bytes of a hosted image
const bytes = await env.IMAGES.hosted.image("IMAGE_ID").bytes();
// Resize and transcode the image
const response = (
await env.IMAGES.input(bytes)
.transform({ width: 400 })
.output({ format: "image/webp" })
).response();
return response;// Get the raw bytes of a hosted image
const bytes = await env.IMAGES.hosted.image("IMAGE_ID").bytes();
// Resize and transcode the image
const response = (
await env.IMAGES.input(bytes)
.transform({ width: 400 })
.output({ format: "image/webp" })
).response();
return response;Draws an overlay image over another image.
The overlay can be a stream of image bytes or another .input() chain. You can pass a child .transform() function inside this method to resize or manipulate the overlay before drawing it.
Accepts opacity, repeat, a side (left, right, top, bottom), and composite. For the full list of draw options and examples, refer to Draw overlays and watermarks.
Generates the final image with the specified output options.
Accepts the following options:
format Encodes the image in a supported format, such as AVIF, WebP, or JPEG. This method is required there is no default output format.quality Specifies the output quality of an image for JPEG, WebP, and AVIF formats, expressed as a fixed value or perceptual quality level.anim Specifies whether to preserve animation frames from input files. Set anim:false to convert animations to still images.const response = (
await env.IMAGES.input(stream)
.transform({ rotate: 90 })
.transform({ width: 128 })
.transform({ blur: 20 })
.output({ format: "image/avif" })
).response();
return response;const response = (
await env.IMAGES.input(stream)
.transform({ rotate: 90 })
.transform({ width: 128 })
.transform({ blur: 20 })
.output({ format: "image/avif" })
).response();
return response;Outputs information about the image, such as format, fileSize, width, and height.
The Images API can be used in local development through Wrangler, 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:
To test the low-fidelity version of Images, you can run wrangler dev:
npx wrangler dev
Currently, this version supports only width, height, rotate, and format.
To test the high-fidelity remote version of Images, you can use the --remote flag:
npx wrangler dev --remote
When testing with the Workers Vitest integration, the low-fidelity offline version is used by default, to avoid hitting the Cloudflare API in tests.
| Web Proxy Viewer | New URL | Original Page |