diff --git a/astro/src/lib/images.ts b/astro/src/lib/images.ts index 6cc96ac..10d7549 100644 --- a/astro/src/lib/images.ts +++ b/astro/src/lib/images.ts @@ -1,3 +1,22 @@ export function getImageUrl(url: string) { return `${import.meta.env.DIRECTUS_URL}assets/${url}`; } + +export function getImageSize(width: number, height: number, targetMegapixels: number): ResizedImageResponse { + const originalPixels = width * height; + const targetPixels = targetMegapixels * 1000 * 1000; + + if (originalPixels >= targetPixels) { + return { + width, + height + } + } + + const scale = Math.sqrt(targetPixels / originalPixels); + + return { + width: Math.round(scale * width), + height: Math.round(scale * height) + } +} diff --git a/astro/src/types/common/images.d.ts b/astro/src/types/common/images.d.ts index 758b372..c196622 100644 --- a/astro/src/types/common/images.d.ts +++ b/astro/src/types/common/images.d.ts @@ -3,3 +3,8 @@ type PhotoProps = { width: number; height: number; } + +type ResizedImageResponse = { + width: number; + height: number; +}