Skip to content

Instantly share code, notes, and snippets.

@eelke
Last active Nov 25, 2021
Embed
What would you like to do?
Fast ProcessWire images to WebP converter
#!/bin/bash
#########################################################################################################
#
# Fast Recursive Images to WebP converter
# Customized for ProcessWire CMS/CMF <https://www.processwire.com>
#
# Author: Eelke Feenstra <dev@eelke.net>
# Version: 001
# Based upon: https://github.com/onadrog/bash-webp-converter
#
# Quick & dirty script to add webp versions to all PNG / JPG / JPEG files inside your PW assets folder
#
# 1. Set this script to executable:
# $ chmod +x convert-webp.sh
#
# 2. Check if cwebp is installed:
# $ cwebp -version
# If it is not, install:
# $ brew install webp
# Or follow instructions https://developers.google.com/speed/webp/download
# and change $executable to cwebp's full path
#
# 3. Run the script directly on a folder:
# $ ./convert-webp.sh /path/to/your/folder
#
#########################################################################################################
# Configuration
executable="cwebp" # update this to reflect your installation!
quality=90 # change to desired WebP quality
#########################################################################################################
converted=0
skipped=0
echo "Entering $1"
for file in $1/*
do
name="${file%.*}"
echo "FILE: $file"
echo "NAME: $name"
# Skip the folder itself..
if [ "$name" = "./." ]; then
echo "SKIP: $name"
continue;
fi
if [[ $(file --mime-type -b $name.webp) == image/webp ]]; then
echo "FOUND: $name.webp, skipping.."
skipped=$((skipped+1))
elif [[ $(file --mime-type -b $file) == image/*g ]]; then
echo "NOT FOUND: $name.webp"
newfile(){
echo "$file" | sed -r 's/(\.[a-z0-9]*$)/.webp/'
}
$executable -q $quality "$file" -short -o "$(newfile)"
converted=$((converted+1))
fi
done
echo "Converted $converted, Skipped $skipped"
#!/bin/bash
#########################################################################################################
#
# HOWTO quickly convert all ProcessWire images to webp
# <https://www.processwire.com>
# Run copnvert-webp.sh recursively on your entire assets folder.
# 1. Do a quick run to make sure you have the right base folder (should print all file folders):
# $ find path/to/processwire/site/assets/files -maxdepth 2 -type d
#
# 2. Then point to the converter using xargs
# $ find path/to/processwire/site/assets/files -maxdepth 2 -type d | xargs -I '{}' ./convert-webp.sh '{}'
#
# Good luck!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment