mat.services/nix/optimize-images.nix

18 lines
700 B
Nix
Raw Normal View History

2022-10-17 00:50:53 +00:00
{ bc, libwebp, scour, writeShellScriptBin }:
writeShellScriptBin "optimize-images" ''
shopt -s globstar nullglob
${scour}/bin/scour -i static/image/_favicon.svg -o static/image/favicon.svg
for image in content/**/_*.png static/image/**/_*.png; do
path=$(dirname $image)
2022-10-17 00:50:53 +00:00
file=$(basename -s .png $image)
newimage=$path/''${file:1}.webp
echo "optimizing $image"
2022-10-17 00:50:53 +00:00
${libwebp}/bin/cwebp -quiet -o $newimage $image
oldsize=$(stat --format=%s $image)
newsize=$(stat --format=%s $newimage)
pct=$(${bc}/bin/bc <<< "scale=1; $newsize * 100 / $oldsize")
echo "size went from "$(($oldsize / 1024))"KB to "$(($newsize / 1024))"KB ("$pct"% as large as original)"
done
''