--- import { type ImgAttributes, getImage } from 'astro:assets' import type { ImageMetadata, ImageOutputFormat } from 'astro' import styles from './index.module.css' type Props = ImgAttributes & { src: ImageMetadata width: number height?: number class?: string objectFit?: boolean } const { title, alt, src, class: className, width, height, objectFit } = Astro.props const formats: ImageOutputFormat[] = ['webp', 'jpg'] const sizes = [ Math.round(Number(width) / 2), Number(width), Math.round(Number(width) * 2) ] const originalSize = src.width const images: { format: ImageOutputFormat; size: number; src: string }[] = [] const srcFallback = await getImage({ src, width: Math.round(Number(width) / 2), format: 'jpg' }) for (const format of formats) { for (const size of sizes) { const image = await getImage({ src, width: size, format, quality: 75 }) // Check if the original image is smaller than the target size, // if the image is smaller, add the original size instead images.push({ format, size: originalSize >= size ? size : originalSize, src: image.src }) } } // Generate srcSet strings const srcSetStrings = formats.reduce((acc: Record, format) => { const filteredImages = images.filter((img) => img.format === format) acc[format] = filteredImages .map((img) => `${img.src} ${img.size}w`) .join(', ') return acc }, {}) ---
{ objectFit ? (
) : null } { formats .filter((format) => format !== 'jpg') .map((format) => ( )) } {alt} {title &&
{title}
}