Home / Function/ transform() — astro Function Reference

transform() — astro Function Reference

Architecture documentation for the transform() function in image-binding-transform.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  8cb447de_ae3e_a32b_2962_01b50d9b2e4d["transform()"]
  e6a86237_c7e3_5b64_faf5_c771aa8ce203["image-binding-transform.ts"]
  8cb447de_ae3e_a32b_2962_01b50d9b2e4d -->|defined in| e6a86237_c7e3_5b64_faf5_c771aa8ce203
  style 8cb447de_ae3e_a32b_2962_01b50d9b2e4d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/cloudflare/src/utils/image-binding-transform.ts lines 6–43

export async function transform(
	rawUrl: string,
	images: ImagesBinding,
	assets: Fetcher,
): Promise<Response> {
	const url = new URL(rawUrl);

	const href = url.searchParams.get('href');

	if (!href || (isRemotePath(href) && !isRemoteAllowed(href, imageConfig))) {
		return new Response('Forbidden', { status: 403 });
	}

	const imageSrc = new URL(href, url.origin);
	const content = await (isRemotePath(href) ? fetch(imageSrc) : assets.fetch(imageSrc));
	if (!content.body) {
		return new Response(null, { status: 404 });
	}
	const input = images.input(content.body);

	const format = url.searchParams.get('f');

	if (!format || !['avif', 'webp', 'jpeg'].includes(format)) {
		return new Response(`The "${format}" format is not supported`, { status: 400 });
	}

	return (
		await input
			.transform({
				width: url.searchParams.has('w') ? parseInt(url.searchParams.get('w')!) : undefined,
				height: url.searchParams.has('h') ? parseInt(url.searchParams.get('h')!) : undefined,
				// `quality` is documented, but doesn't appear to work in manual testing...
				// quality: url.searchParams.get('q'),
				fit: url.searchParams.get('fit') as ImageTransform['fit'],
			})
			.output({ format: `image/${format as 'webp' | 'avif' | 'jpeg'}` })
	).response();
}

Domain

Subdomains

Frequently Asked Questions

What does transform() do?
transform() is a function in the astro codebase, defined in packages/integrations/cloudflare/src/utils/image-binding-transform.ts.
Where is transform() defined?
transform() is defined in packages/integrations/cloudflare/src/utils/image-binding-transform.ts at line 6.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free