kjelsrud.dev/node_modules/astro/dist/assets/utils/metadata.js

29 lines
663 B
JavaScript
Raw Normal View History

2023-07-19 21:31:30 +02:00
import fs from "node:fs/promises";
import { fileURLToPath } from "node:url";
import imageSize from "../vendor/image-size/index.js";
async function imageMetadata(src, data) {
let file = data;
if (!file) {
try {
file = await fs.readFile(src);
} catch (e) {
return void 0;
}
}
const { width, height, type, orientation } = imageSize(file);
const isPortrait = (orientation || 0) >= 5;
if (!width || !height || !type) {
return void 0;
}
return {
src: fileURLToPath(src),
width: isPortrait ? height : width,
height: isPortrait ? width : height,
format: type,
orientation
};
}
export {
imageMetadata
};