Created
November 13, 2024 04:36
-
-
Save WomB0ComB0/10b186cf8080562cfd5334c683ad851e to your computer and use it in GitHub Desktop.
bs64 image hasher javascript
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
"use strict"; | |
const sharp = require("sharp"); | |
const fs = require("fs"); | |
const path = require("path"); | |
sharp.cache(false); | |
async function generateLazyImage(src) { | |
const { default: got } = await import("got"); | |
const response = await got(src, { responseType: "buffer" }); | |
const body = response.body; | |
const sharpImage = sharp(body); | |
const metadata = await sharpImage.metadata(); | |
const format = metadata.format; | |
const lqipBuf = await sharpImage | |
.resize({ width: 30, height: 30, fit: "inside" }) | |
.toBuffer(); | |
const lqip = `data:image/${format};base64,${lqipBuf.toString("base64")}`; | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(lqip); | |
}, 1000); | |
}); | |
} | |
const directory = path.join(__dirname, 'public/<...>'); | |
const files = fs.readdirSync(directory); | |
console.log(files) | |
const images = files.map(file => `http://localhost:3000/<...>/${file}`); | |
console.log(images) | |
for (const image of images) { | |
generateLazyImage(image).then((lqip) => { | |
console.log(lqip); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment