fix: actually check image sizes
Some checks failed
Deploy to SFTP Server / build (push) Failing after 2m24s

This commit is contained in:
Max Richter
2025-10-28 15:35:43 +01:00
parent 71074a8b49
commit c3299868c0

View File

@@ -1,7 +1,7 @@
---
import type { ImageMetadata } from "astro";
import { Picture as AstroImage } from "astro:assets";
import { inferRemoteSize } from "astro/assets/utils";
import { inferRemoteSize, inferSize } from "astro/assets/utils";
import { getProcessedImage } from "@helpers/image";
interface Props {
src: ImageMetadata & { fsPath?: string; src?: string };
@@ -15,11 +15,13 @@ interface Props {
thumbnail?: boolean;
}
async function checkImage(image: ImageMetadata):Promise<({height:number,width:number}|undefined)> {
async function checkImage(
image: ImageMetadata,
): Promise<{ height: number; width: number } | undefined> {
const src = typeof image === "string" ? image : image.src;
if (!src) return ;
if (!src) return;
try {
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return;
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return {};
const res = await inferRemoteSize(src);
if (res.format) {
image.format = res.format;
@@ -27,11 +29,11 @@ async function checkImage(image: ImageMetadata):Promise<({height:number,width:nu
} else {
console.log("Failed to load: ", src);
}
return ;
return;
} catch (err) {
console.log("\n");
console.log("Failed to fetch: ", src);
return ;
return;
}
}
@@ -81,11 +83,10 @@ const sizes = thumbnail
alt={alt}
data-thumbhash={thumbhash}
data-exif={JSON.stringify(exif)}
inferSize={true}
width={imageOk?.width}
height={imageOk?.height}
pictureAttributes={{
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
}}
class={`${Astro.props.class} h-full w-full`}
widths={sizes.map((size) => size.width)}
@@ -94,5 +95,7 @@ const sizes = thumbnail
.join(", ")}>
<slot />
</AstroImage>
) : <div>{JSON.stringify({ image })}</div>
) : (
<div>{JSON.stringify({ "imageOk":imageOk, image })}</div>
)
}