Skip to content

Commit

Permalink
Clone image before running lqip op
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodevx committed Aug 26, 2023
1 parent 8f41fce commit e9b1a68
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lib/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ function run(cfg) {
const lqip = (cfg && parseInt(cfg)) ?? 16
if (lqip) {
const { image } = metadatas.find((i) => i.src === pic.img.src)
let data
if (lqip > 1) {
const buf = await image.resize({ width: lqip }).toFormat('webp', { quality: 20 }).toBuffer()
data = buf.toString('base64')
const buf = await image
.clone()
.resize({ width: lqip })
.toFormat('webp', { quality: 20 })
.toBuffer()
pic.img.lqip = buf.toString('base64')
} else {
const { dominant } = await image.stats()
const { r, g, b } = dominant
data = '#' + ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)
pic.img.lqip = '#' + ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)
}
pic.img.lqip = data
}
return pic
}
Expand Down

0 comments on commit e9b1a68

Please sign in to comment.