polymech-astro/packages/imagetools/api/utils/imagetools.js
2025-08-26 12:17:29 +02:00

42 lines
848 B
JavaScript

// @ts-check
import {
builtins,
loadImage,
applyTransforms,
generateTransforms,
} from "imagetools-core";
export {
loadImage
} from "imagetools-core";
export async function getImageDetails(path, width, height, aspect) {
const loadedImage = loadImage(path)
if (aspect && !width && !height) {
if (!width && !height) {
({ width } = await loadedImage.metadata());
}
if (width) {
height = width / aspect;
}
if (height) {
width = height * aspect;
}
}
const { image, metadata } = await applyTransforms(
generateTransforms({ width, height }, builtins).transforms,
loadedImage
);
const {
width: imageWidth,
height: imageHeight,
format: imageFormat,
} = metadata;
return { image, imageWidth, imageHeight, imageFormat };
}