This repository has been archived on 2025-12-24. You can view files and clone it, but cannot push or open issues or pull requests.
site-template/packages/imagetoolsOSR/api/utils/codecs.js
2025-03-07 14:59:06 +01:00

37 lines
820 B
JavaScript

// @ts-check
import fs from "node:fs";
import { extname } from "node:path";
import * as codecs from "@astropub/codecs";
export async function getImageDetails(path, width, height, aspect) {
const extension = extname(path).slice(1);
const imageFormat = extension === "jpeg" ? "jpg" : extension;
const buffer = fs.readFileSync(path);
const decodedImage = await codecs.jpg.decode(buffer);
if (aspect && !width && !height) {
if (!width && !height) {
({ width } = decodedImage);
}
if (width) {
height = width / aspect;
}
if (height) {
width = height * aspect;
}
}
const image = await decodedImage.resize({ width, height });
const { width: imageWidth, height: imageHeight } = image;
return {
image,
imageWidth,
imageHeight,
imageFormat,
};
}