Create function to calculate the new width and height of an image
This commit is contained in:
@@ -1,3 +1,22 @@
|
|||||||
export function getImageUrl(url: string) {
|
export function getImageUrl(url: string) {
|
||||||
return `${import.meta.env.DIRECTUS_URL}assets/${url}`;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
5
astro/src/types/common/images.d.ts
vendored
5
astro/src/types/common/images.d.ts
vendored
@@ -3,3 +3,8 @@ type PhotoProps = {
|
|||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ResizedImageResponse = {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user