Skip to content

Commit

Permalink
add webp support to optimise-images script
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Aug 10, 2024
1 parent af0d724 commit cd2c6a8
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions scripts/optimise-images.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env sh
set -e -u

# This script optimises png and jpeg images on the site and marks them as optimised, so that they are not processed again.
# It uses optipng and advancecomp for PNGs and jpegoptim for JPEGs.
# This script optimises png, jpeg, and webp images on the site and marks them as optimised, so that they are not processed again.
# It uses optipng and advancecomp for PNGs, jpegoptim for JPEGs, and cwebp for WebP images.
# The script is designed to be run as a cron job.
# It only processes files that have been modified since the last optimisation.
# It also cleans up orphaned .optimised files.

# Install missing packages
sudo apt -y install optipng advancecomp jpegoptim
sudo apt -y install optipng advancecomp jpegoptim webp

# Function to optimise PNGs
optimise_png() {
Expand Down Expand Up @@ -36,6 +36,20 @@ optimise_jpeg() {
fi
}

# Function to optimise WebP images
optimise_webp() {
local file="$1"
local tmpfile="${file}.tmp.webp"
if nice -n 10 ionice -c2 -n7 cwebp -q 80 "$file" -o "$tmpfile"; then
mv "$tmpfile" "$file"
touch -r "$file" "$file.optimised" # Set .optimised file's modification time to the original file's time
chmod 600 "$file.optimised" # Restrict permissions to owner only
else
echo "Error: Failed to process $file with cwebp. Skipping."
rm -f "$tmpfile" # Clean up temporary file if optimization fails
fi
}

# Clean up orphaned .optimised files
cleanup_optimised_flags() {
find web/prod/images web/prod/upload -type f -iname "*.optimised" | while read -r flag; do
Expand All @@ -46,19 +60,23 @@ cleanup_optimised_flags() {
done
}

# Optimising PNG files
echo "Optimising PNGs..."
find web/prod/images web/prod/upload -type f -iname "*.png" | while read -r png; do
if [ ! -f "${png}.optimised" ] || [ "$png" -nt "${png}.optimised" ]; then
optimise_png "$png"
fi
done

# Optimising JPEG files
echo "Optimising JPEGs..."
find web/prod/images web/prod/upload -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | while read -r jpg; do
if [ ! -f "${jpg}.optimised" ] || [ "$jpg" -nt "${jpg}.optimised" ]; then
optimise_jpeg "$jpg"
# Single find command to locate PNG, JPEG, and WebP files, case insensitive
echo "Optimising images..."
find web/prod/images web/prod/upload -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.webp" \) | while read -r file; do
if [ ! -f "${file}.optimised" ] || [ "$file" -nt "${file}.optimised" ]; then
# Convert file extension to lowercase
extension=$(echo "${file##*.}" | tr '[:upper:]' '[:lower:]')
case "$extension" in
png)
optimise_png "$file"
;;
jpg|jpeg)
optimise_jpeg "$file"
;;
webp)
optimise_webp "$file"
;;
esac
fi
done

Expand All @@ -67,4 +85,3 @@ echo "Cleaning up orphaned .optimised files..."
cleanup_optimised_flags

echo "Images optimisation and cleanup complete"

0 comments on commit cd2c6a8

Please sign in to comment.