From 360dc2b055c6c44431de0668491558c0c3f0818d Mon Sep 17 00:00:00 2001 From: Nicholas Lamicela Date: Sat, 30 Nov 2024 19:35:41 -0800 Subject: [PATCH] script for manually shrinking images --- .gitignore | 4 ++++ package.json | 4 +++- tools/img-shrink.js | 27 +++++++++++++++++++++++++++ tools/in/.gitkeep | 0 tools/out/.gitkeep | 0 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tools/img-shrink.js create mode 100644 tools/in/.gitkeep create mode 100644 tools/out/.gitkeep diff --git a/.gitignore b/.gitignore index ffb37de..057d1ae 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ node_modules/ package-lock.json .cache patches +tools/in/* +!tools/in/.gitkeep +tools/out/* +!tools/out/.gitkeep diff --git a/package.json b/package.json index aae00f5..0d8db51 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "start": "cross-env NODE_ENV=localhost npx @11ty/eleventy --serve --quiet", "debug": "cross-env DEBUG=Eleventy* npx @11ty/eleventy", "debugstart": "cross-env DEBUG=Eleventy* npx @11ty/eleventy --serve --quiet", - "benchmark": "cross-env DEBUG=Eleventy:Benchmark* npx @11ty/eleventy" + "benchmark": "cross-env DEBUG=Eleventy:Benchmark* npx @11ty/eleventy", + "img2k": "node tools/img-shrink.js" }, "repository": { "type": "git", @@ -39,6 +40,7 @@ "@11ty/eleventy-plugin-rss": "^1.2.0", "@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0", "cross-env": "^7.0.3", + "gm": "^1.22.0", "luxon": "^3.3.0", "markdown-it-anchor": "^8.6.7" } diff --git a/tools/img-shrink.js b/tools/img-shrink.js new file mode 100644 index 0000000..09a4a13 --- /dev/null +++ b/tools/img-shrink.js @@ -0,0 +1,27 @@ +// winget install ImageMagick.Q8 +const { exec } = require('node:child_process'); +const { readdir } = require('node:fs/promises'); +const path = require('node:path'); + +async function main() { + const dirIn = 'tools/in'; + const dirOut = 'tools/out'; + for (const base of await readdir(dirIn)) { + if (base === '.gitkeep') continue; + const fileIn = `${dirIn}/${base}`; + const file = path.parse(fileIn); + const fileOut = `${dirOut}/${file.name}.webp`; + + const lossless = file.ext === '.png'; + const cmd = [ + `magick "${fileIn}"`, + `-define webp:lossless=${lossless}`, + `-define webp:target-size=200kb`, + `"${fileOut}"`, + ].join(' '); + console.log(cmd); + exec(cmd, (_err, stdout, stderr) => console.log(stdout + stderr)); + } +} + +main(); diff --git a/tools/in/.gitkeep b/tools/in/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tools/out/.gitkeep b/tools/out/.gitkeep new file mode 100644 index 0000000..e69de29