aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-03-18 20:38:04 -0500
committermat <github@matdoes.dev>2022-03-18 20:38:04 -0500
commitdf9fc83ad814ba93f162e22c3c5f552eb627414c (patch)
tree666bd88828a680b53afca7b214c4bf94f0929705 /scripts
parentcdb32f8663b687067b2ab835edbca420100972b2 (diff)
downloadskyblock-stats-df9fc83ad814ba93f162e22c3c5f552eb627414c.tar.gz
skyblock-stats-df9fc83ad814ba93f162e22c3c5f552eb627414c.tar.bz2
skyblock-stats-df9fc83ad814ba93f162e22c3c5f552eb627414c.zip
Resize background images for customization page
Diffstat (limited to 'scripts')
-rw-r--r--scripts/updateBackgrounds.js44
1 files changed, 41 insertions, 3 deletions
diff --git a/scripts/updateBackgrounds.js b/scripts/updateBackgrounds.js
index e2c74f2..425e2fe 100644
--- a/scripts/updateBackgrounds.js
+++ b/scripts/updateBackgrounds.js
@@ -1,10 +1,48 @@
-import fs from 'fs'
+import { promises as fs } from 'fs'
+import { ImagePool } from '@squoosh/lib'
+import { cpus } from 'os'
+
+const imagePool = new ImagePool(cpus().length)
// read the file names in the backgrounds folder
-const backgrounds = fs.readdirSync('static/backgrounds')
+const backgrounds = await fs.readdir('static/backgrounds')
-await fs.promises.writeFile(
+await fs.writeFile(
'src/_backgrounds.json',
JSON.stringify(backgrounds),
{ encoding: 'utf8' }
)
+
+// 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
+
+ await fs.writeFile(`static/backgrounds-small/${name}`, rawEncodedImage.binary)
+}
+
+try {
+ await fs.rm('static/backgrounds-small', { recursive: true })
+} catch {
+ // it errors if the directory already doesn't exist
+}
+await fs.mkdir('static/backgrounds-small', { recursive: true })
+
+await Promise.all(backgrounds.map(b => resizeBackground(b)))
+await imagePool.close()