Optimize with Workers
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:
- Optimize an image stored in Images or R2 by passing the bytes directly, instead of fetching through a public URL.
- Resize an image, overlay a watermark, then resize the combined output into a final result — all in a single chain of operations.
- Control the order of operations for optimization parameters. When you use the URL interface, the optimization parameters follow a fixed order of operations.
Bindings can be configured in the Cloudflare dashboard for your Worker or in the Wrangler configuration file in your project's directory.
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).
Creates an optimization handle for an image. All operations begin with this method. Accepts image bytes 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 imageconst bytes = await env.IMAGES.hosted.image("IMAGE_ID").bytes();
// Resize and transcode the imageconst response = ( await env.IMAGES.input(bytes) .transform({ width: 400 }) .output({ format: "image/webp" })).response();
return response;// Get the raw bytes of a hosted imageconst bytes = await env.IMAGES.hosted.image("IMAGE_ID").bytes();
// Resize and transcode the imageconst 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, and a side (left, right, top, bottom). 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. Setanim:falseto 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:
- 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 offline version that supports only a subset of features, such as resizing and rotation.
To test the low-fidelity version of Images, you can run wrangler dev:
npx wrangler devCurrently, 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 --remoteWhen testing with the Workers Vitest integration, the low-fidelity offline version is used by default, to avoid hitting the Cloudflare API in tests.