Skip to content

Instantly share code, notes, and snippets.

@Muzammil-cyber
Created April 28, 2024 10:17
Show Gist options
  • Save Muzammil-cyber/9765bd01c33ac5d5ebf58ba5dedf517c to your computer and use it in GitHub Desktop.
Save Muzammil-cyber/9765bd01c33ac5d5ebf58ba5dedf517c to your computer and use it in GitHub Desktop.
import path from "node:path";
import fs from "node:fs/promises";
import { getPlaiceholder } from "plaiceholder";
export 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 },
};
};
// Usage
// const { base64, img } = await getImage("/assets/image/example.jpg");
export 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 },
};
};
// Usage
// const { base64, img } = await getImage(
// "https://images.unsplash.com/photo-1621961458348-f013d219b50c?auto=format&fit=crop&w=2850&q=80"
// );
@Muzammil-cyber
Copy link
Author

Need:
npm i plaiceholder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment