36 lines
858 B
JavaScript
36 lines
858 B
JavaScript
// @ts-check
|
|
import {
|
|
builtins,
|
|
applyTransforms,
|
|
generateTransforms,
|
|
} from "imagetools-core"
|
|
import sharp from "sharp"
|
|
|
|
export const loadImage = async (path) => { return await sharp(path) }
|
|
|
|
export async function getImageDetails(path, width, height, aspect) {
|
|
const loadedImage = await 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, new URLSearchParams()).transforms,loadedImage
|
|
)
|
|
const {
|
|
width: imageWidth,
|
|
height: imageHeight,
|
|
format: imageFormat,
|
|
} = metadata
|
|
return { image, imageWidth, imageHeight, imageFormat }
|
|
}
|