Edge Platform
Image Resizer
Resize remote images with Cloudflare Image Resizing from a Worker - on-the-fly thumbnails and responsive variants
Fetch and resize remote images using Cloudflare Image Resizing via the fetch API and cf.image options. Returns the resized image binary with the appropriate Content-Type header.
API Reference
GET /resize
Fetches a remote image and returns a resized version.
Prop
Type
Example Request
curl "https://your-worker.workers.dev/resize?url=https://example.com/image.jpg&width=800&fit=scale-down" --output resized.jpgSuccess Response
Returns the resized image as binary data with an appropriate Content-Type header (e.g. image/jpeg, image/png).
Error Response
{
"error": "Missing or invalid query parameter: url",
"code": "INVALID_URL"
}Error Codes
400- Missing or invalidurl(INVALID_URL)400- Invalidwidthvalue (INVALID_WIDTH)400- Invalidheightvalue (INVALID_HEIGHT)400- Neitherwidthnorheightprovided (MISSING_DIMENSION)400- Invalidfitmode (INVALID_FIT)502- Upstream fetch or resize failed (FETCH_ERROR)
Use Cases
- Serve dynamically resized thumbnails from any remote image URL
- Learn Cloudflare Image Resizing options via Workers
fetch - Build on-the-fly image proxy endpoints at the edge
- Reduce bandwidth by resizing images before delivery to clients
Limitations
- Remote image fetch is subject to a timeout
- Resizing options are limited to the query params exposed by this experiment
- Origin must allow Cloudflare to fetch the image URL
- Single image per request; no batch processing
Use in your project
Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/image-resizer.
DependenciesNoneBindingsNonePlatformWorkers runtime
import { DEFAULT_FIT, MAX_DIMENSION, VALID_FITS, type ImageFit } from "../constants/defaults";export function parseDimension(input: string | undefined): number | null { if (input === undefined) return null; if (typeof input !== "string") return null; const trimmed = input.trim(); if (!trimmed) return null; const value = Number(trimmed); if (!Number.isInteger(value) || value <= 0 || value > MAX_DIMENSION) return null; return value;}export function normalizeFit(input: string | undefined): ImageFit | null { const raw = (input ?? DEFAULT_FIT).trim().toLowerCase(); if (VALID_FITS.includes(raw as ImageFit)) { return raw as ImageFit; } return null;}export function buildImageOptions( width: number | null, height: number | null, fit: ImageFit): NonNullable<RequestInitCfProperties["image"]> { const options: NonNullable<RequestInitCfProperties["image"]> = { fit }; if (width !== null) options.width = width; if (height !== null) options.height = height; return options;}Deployment
Deploy
Image Resizing must be enabled on your Cloudflare zone. On workers.dev URLs, resizing may not be available.
Test your deployment
curl "https://your-worker.workers.dev/resize?url=https://example.com/image.jpg&width=800&fit=scale-down" --output resized.jpgLocal Development
cd apps/experiments/image-resizer
npm install
npm run devTest locally:
curl "http://localhost:8787/resize?url=https://example.com/image.jpg&width=800&fit=scale-down" --output resized.jpgCloudflare Features Used
- Workers - Edge compute runtime
- Image Resizing -
fetchwithcf.imagetransform options - Fetch API - Remote image retrieval