Home / Function/ sharpService.transform() — astro Function Reference

sharpService.transform() — astro Function Reference

Architecture documentation for the sharpService.transform() function in sharp.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  65162a20_def3_0daf_b4e0_c43d00330756["sharpService.transform()"]
  e98aa86d_990d_0999_8b69_e22d93343858["sharp.ts"]
  65162a20_def3_0daf_b4e0_c43d00330756 -->|defined in| e98aa86d_990d_0999_8b69_e22d93343858
  5df770ae_ba54_ac95_82f9_d5dd045489ae["loadSharp()"]
  65162a20_def3_0daf_b4e0_c43d00330756 -->|calls| 5df770ae_ba54_ac95_82f9_d5dd045489ae
  style 65162a20_def3_0daf_b4e0_c43d00330756 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/services/sharp.ts lines 63–151

	async transform(inputBuffer, transformOptions, config) {
		if (!sharp) sharp = await loadSharp();
		const transform: BaseServiceTransform = transformOptions as BaseServiceTransform;
		const kernel = config.service.config.kernel;

		// Return SVGs as-is
		// TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service.
		if (transform.format === 'svg') return { data: inputBuffer, format: 'svg' };

		const result = sharp(inputBuffer, {
			failOnError: false,
			pages: -1,
			limitInputPixels: config.service.config.limitInputPixels,
		});

		// always call rotate to adjust for EXIF data orientation
		result.rotate();

		// get some information about the input
		const { format } = await result.metadata();

		// If `fit` isn't set then use old behavior:
		// - Do not use both width and height for resizing, and prioritize width over height
		// - Allow enlarging images

		if (transform.width && transform.height) {
			const fit: keyof FitEnum | undefined = transform.fit
				? (fitMap[transform.fit] ?? 'inside')
				: undefined;

			result.resize({
				width: Math.round(transform.width),
				height: Math.round(transform.height),
				kernel,
				fit,
				position: transform.position,
				withoutEnlargement: true,
			});
		} else if (transform.height && !transform.width) {
			result.resize({
				height: Math.round(transform.height),
				withoutEnlargement: true,
				kernel,
			});
		} else if (transform.width) {
			result.resize({
				width: Math.round(transform.width),
				withoutEnlargement: true,
				kernel,
			});
		}

		// If background is set, flatten the image with the specified background.
		// We do this after resize to ensure the background covers the entire image
		// even if its size has expanded.
		if (transform.background) {
			result.flatten({ background: transform.background });
		}

		if (transform.format) {
			let quality: number | string | undefined = undefined;
			if (transform.quality) {
				const parsedQuality = parseQuality(transform.quality);
				if (typeof parsedQuality === 'number') {
					quality = parsedQuality;
				} else {
					quality = transform.quality in qualityTable ? qualityTable[transform.quality] : undefined;
				}
			}

			if (transform.format === 'webp' && format === 'gif') {
				// Convert animated GIF to animated WebP with loop=0 (infinite)
				result.webp({ quality: typeof quality === 'number' ? quality : undefined, loop: 0 });
			} else {
				result.toFormat(transform.format as keyof FormatEnum, { quality });
			}
		}

		const { data, info } = await result.toBuffer({ resolveWithObject: true });

		// Sharp can sometimes return a SharedArrayBuffer when using WebAssembly.

Domain

Subdomains

Calls

Frequently Asked Questions

What does sharpService.transform() do?
sharpService.transform() is a function in the astro codebase, defined in packages/astro/src/assets/services/sharp.ts.
Where is sharpService.transform() defined?
sharpService.transform() is defined in packages/astro/src/assets/services/sharp.ts at line 63.
What does sharpService.transform() call?
sharpService.transform() calls 1 function(s): loadSharp.

Analyze Your Own Codebase

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

Try Supermodel Free