Create function to calculate the new width and height of an image

This commit is contained in:
itsfinniii
2026-04-04 18:36:00 +02:00
parent cf12428f98
commit 47e50a3ba4
2 changed files with 24 additions and 0 deletions

View File

@@ -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)
}
}

View File

@@ -3,3 +3,8 @@ type PhotoProps = {
width: number;
height: number;
}
type ResizedImageResponse = {
width: number;
height: number;
}