Files
website/src/components/Image.astro
Max Richter b32ca7d65e
All checks were successful
Deploy to SFTP Server / build (push) Successful in 4m6s
feat: improve some error messages
2025-05-14 19:48:55 +02:00

63 lines
1.3 KiB
Plaintext

---
import type { ImageMetadata } from "astro";
import { Picture as AstroImage } from "astro:assets";
import { generateThumbHash, getExifData } from "@helpers/image";
interface Props {
src: ImageMetadata & { fsPath?: string };
alt: string;
pictureClass?: string;
class?: string;
caption?: string;
hash?: boolean;
loader?: boolean;
maxWidth?: number;
}
const {
src: image,
loader = true,
pictureClass = "",
hash = true,
alt,
maxWidth,
} = Astro.props;
let thumbhash = hash && image.fsPath ? await generateThumbHash(image) : "";
let exif = await getExifData(image);
const sizes = [
{
width: 240,
media: "(max-width: 360px)",
},
{
width: 540,
media: "(max-width: 720px)",
},
{
width: 720,
media: "(max-width: 1600px)",
},
{
width: image.width,
},
].filter((size) => !maxWidth || size.width <= maxWidth);
---
<AstroImage
src={image}
alt={alt}
data-thumbhash={thumbhash}
data-exif={JSON.stringify(exif)}
pictureAttributes={{
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
}}
class={Astro.props.class}
widths={sizes.map((size) => size.width)}
sizes={sizes
.map((size) => `${size.media || "100vw"} ${size.width}px`)
.join(", ")}>
<slot />
</AstroImage>