Created
April 28, 2024 10:17
-
-
Save Muzammil-cyber/9765bd01c33ac5d5ebf58ba5dedf517c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
// ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need:
npm i plaiceholder