From 327d2a391cd442024ac039253c7a696cc8a40b45 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 15 Dec 2022 19:50:18 -0600 Subject: replace squooshlib with sharp --- scripts/updateBackgrounds.js | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'scripts/updateBackgrounds.js') diff --git a/scripts/updateBackgrounds.js b/scripts/updateBackgrounds.js index 0c3e214..a5467fa 100644 --- a/scripts/updateBackgrounds.js +++ b/scripts/updateBackgrounds.js @@ -1,9 +1,7 @@ import { promises as fs } from 'fs' -import { ImagePool } from '@squoosh/lib' +import sharp from 'sharp' import { cpus } from 'os' -const imagePool = new ImagePool(cpus().length) - // read the file names in the backgrounds folder const backgrounds = await fs.readdir('static/backgrounds') // sort by natural order @@ -18,25 +16,13 @@ await fs.writeFile( // resize the backgrounds async function resizeBackground(name) { - const file = await fs.readFile(`static/backgrounds/${name}`) - const image = imagePool.ingestImage(file) - - const preprocessOptions = { - resize: { - width: 512, - }, - } - await image.preprocess(preprocessOptions) - - await image.encode({ - mozjpeg: { - quality: 30, - }, - }) - - const rawEncodedImage = await image.encodedWith.mozjpeg + const rawEncodedImage = await sharp(`static/backgrounds/${name}`) + .rotate() + .resize(512) + .jpeg({ mozjpeg: true, quality: 30 }) + .toBuffer() - await fs.writeFile(`static/backgrounds-small/${name}`, rawEncodedImage.binary) + await fs.writeFile(`static/backgrounds-small/${name}`, rawEncodedImage) } try { @@ -47,4 +33,3 @@ try { await fs.mkdir('static/backgrounds-small', { recursive: true }) await Promise.all(backgrounds.map(b => resizeBackground(b))) -await imagePool.close() -- cgit