Browse Source

Images are now compressed in parallel for builds

master
Mobius K 9 months ago
parent
commit
de4ff03f42
  1. 4
      Makefile
  2. 31
      compress-image.sh
  3. 35
      compress-images.sh

4
Makefile

@ -67,8 +67,8 @@ build: check-environment clean
npx purifycss $${file} $(public)/**/*.{html,js} --info --min --out $${file} --whitelist "*lightbox*" "*stroke*" ; \
done
# Compress images
./compress-images.sh $(public)
# Compress images in parallel on all available cores
find $(public) -type f \( -name "*.png" -o -name "*jpg" \) | parallel ./compress-image.sh {}
# Add unique keys to build source
uniqueKeys:

31
compress-image.sh

@ -0,0 +1,31 @@
#!/bin/bash
file=$1
# Determine extension and destination for each file
# name="${file%.*}";
extension="${file##*.}";
# Start processing
printf "Input | %s\n" "$(du -h "$file")"
# # Compress the file with Tinify API
# # Deprecated in favor of local tools, but useful every now and then for comparisons
# tinify=$(curl --progress-bar --user api:"${TINIFY_API_KEY}" --data-binary @"$file" https://api.tinify.com/shrink)
# tinifyUrl=$(jq '.output.url' <(echo "$tinify") | tr -d '"')
# curl --progress-bar --user api:"${TINIFY_API_KEY}" --output "$file" "$tinifyUrl"
# CWEBP optimizations
cwebp -q 90 -quiet "$file" -o "$file.webp"
# PNG and JPG specific optimizations
if [ "$extension" = "png" ]; then
pngquant --force --strip --output "$file" "$file"
optipng -force -quiet -strip all "$file"
else
jpegoptim --force --quiet --max=90 --strip-all "$file"
fi
# Output results
printf "%s | %s\n" "$extension" "$(du -h "$file")"
printf "webp | %s\n\n" "$(du -h "$file.webp")"

35
compress-images.sh

@ -1,35 +0,0 @@
#!/bin/bash
inputDir=$1
while read -r file; do
# Determine extension and destination for each file
# name="${file%.*}";
extension="${file##*.}";
# Start processing
printf "Input | %s\n" "$(du -h "$file")"
# # Compress the file with Tinify API
# # Deprecated in favor of local tools, but useful every now and then for comparisons
# tinify=$(curl --progress-bar --user api:"${TINIFY_API_KEY}" --data-binary @"$file" https://api.tinify.com/shrink)
# tinifyUrl=$(jq '.output.url' <(echo "$tinify") | tr -d '"')
# curl --progress-bar --user api:"${TINIFY_API_KEY}" --output "$file" "$tinifyUrl"
# CWEBP optimizations
cwebp -q 90 -quiet "$file" -o "$file.webp"
# PNG and JPG specific optimizations
if [ "$extension" = "png" ]; then
pngquant --force --strip --output "$file" "$file"
optipng -force -quiet -strip all "$file"
else
jpegoptim --force --quiet --max=90 --strip-all "$file"
fi
# Output results
printf "%s | %s\n" "$extension" "$(du -h "$file")"
printf "webp | %s\n\n" "$(du -h "$file.webp")"
done <<< "$(find "$inputDir" -type f \( -name "*.png" -o -name "*jpg" \))"
Loading…
Cancel
Save