import path from "node:path";
import fs from "node:fs/promises";
import { getPlaiceholder } from "plaiceholder";
// For local images in /public (Node.js)
const getImageLocal = async (src: string) => {
const buffer = await fs.readFile(path.join("./public", src));
const { metadata: { height, width }, ...plaiceholder } = await getPlaiceholder(buffer, { size: 10 });
return { ...plaiceholder, img: { src, height, width } };
};
// For remote images (Node.js)
const getImageRemote = async (src: string) => {
const buffer = await fetch(src).then(async (res) =>
Buffer.from(await res.arrayBuffer())
);
const { metadata: { height, width }, ...plaiceholder } = await getPlaiceholder(buffer, { size: 10 });
return { ...plaiceholder, img: { src, height, width } };
};