This guide covers how to optimize images for your Trellis-managed WordPress sites, including Nginx configuration for serving WebP and AVIF images, and methods to convert traditional image formats to these more efficient formats.
- Nginx Configuration for WebP and AVIF
- Image Resizing and Cropping
- Converting Images to WebP
- Converting Images to AVIF
- Automating Image Conversion
- WordPress Plugins
The nginx-includes/webp-avf.conf.j2 file in this repository contains Nginx configuration to automatically serve WebP or AVIF versions of images when the browser supports them. This configuration works by checking the Accept header from the browser and serving the appropriate image format.
When a browser requests an image:
- Nginx checks if the browser accepts WebP or AVIF formats
- If supported formats exist on the server (e.g.,
image.jpg.webporimage.jpg.avif), Nginx serves them instead of the original - If no optimized version exists, the original image is served
- Copy the
nginx-includesdirectory to your Trellis project - Update your Trellis WordPress site configuration to include the custom Nginx configuration:
# group_vars/production/wordpress_sites.yml (or staging/development)
wordpress_sites:
example.com:
# ... existing configuration ...
nginx_includes:
- nginx-includes/webp-avf.conf.j2- Provision your environment to apply the changes:
# For production
trellis provision production
# For staging
trellis provision staging
# For development
trellis provision developmentBefore converting images to WebP or AVIF, you may need to resize or crop them to the appropriate dimensions. ImageMagick is a powerful tool for this purpose.
# macOS (using Homebrew)
brew install imagemagick
# Ubuntu/Debian
sudo apt-get install imagemagickFor detailed instructions on resizing, cropping, and preparing images for conversion, see the Image Resizing and Conversion Guide.
# Resize and crop to 400x400, then convert to WebP
magick input.jpg \
-resize 500x500^ \
-gravity center \
-crop 400x400+0+0 \
-quality 90 \
output.jpg
cwebp -q 85 output.jpg -o output.webpSee RESIZE-AND-CONVERSION.md for more examples, including:
- Creating thumbnails and avatars
- Batch processing multiple images
- Responsive image workflows
- Custom crop offsets
- Quality settings recommendations
The cwebp tool from Google allows you to convert images to WebP format from the command line.
# macOS (using Homebrew)
brew install webp
# Ubuntu/Debian
sudo apt-get install webp# Convert a single image
cwebp -q 80 image.jpg -o image.jpg.webp
# Convert with metadata preservation
cwebp -metadata all -q 80 image.jpg -o image.jpg.webp# Convert all JPG files in current directory
find . -type f -name "*.jpg" -exec cwebp -q 80 {} -o {}.webp \;
# Convert all PNG files in current directory
find . -type f -name "*.png" -exec cwebp -q 80 {} -o {}.webp \;-q 0to-q 100: Lower values = smaller files but lower quality- Recommended: 75-85 for photos, 85-95 for images with text
- See RESIZE-AND-CONVERSION.md for detailed quality comparisons
AVIF offers even better compression than WebP but requires more processing power to create.
# macOS (using Homebrew)
brew install cavif
# From source
git clone https://github.com/kornelski/cavif-rs
cd cavif-rs
cargo install --path .# Convert a single image
cavif --quality 80 image.jpg -o image.jpg.avif
# Convert with specific settings
cavif --quality 80 --speed 5 image.jpg# Convert all JPG files in current directory
find . -type f -name "*.jpg" -exec cavif --quality 80 {} \;For comprehensive automation examples including resizing and cropping, see RESIZE-AND-CONVERSION.md.
Create a script called convert-images.sh:
#!/bin/bash
# Directory containing images to convert
IMAGE_DIR="path/to/images"
QUALITY=80
# Process JPEG images to WebP
find "$IMAGE_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | while read img; do
if [ ! -f "${img}.webp" ]; then
echo "Converting $img to WebP"
cwebp -q $QUALITY "$img" -o "${img}.webp"
fi
if [ ! -f "${img}.avif" ]; then
echo "Converting $img to AVIF"
cavif --quality $QUALITY "$img" -o "${img}.avif"
fi
done
# Process PNG images
find "$IMAGE_DIR" -type f -iname "*.png" | while read img; do
if [ ! -f "${img}.webp" ]; then
echo "Converting $img to WebP"
cwebp -q $QUALITY "$img" -o "${img}.webp"
fi
if [ ! -f "${img}.avif" ]; then
echo "Converting $img to AVIF"
cavif --quality $QUALITY "$img" -o "${img}.avif"
fi
done
echo "Conversion complete!"Make it executable:
chmod +x convert-images.shTo automatically run the conversion script periodically:
# Edit crontab
crontab -e
# Add this line to run daily at 2 AM
0 2 * * * /path/to/convert-images.sh >> /path/to/conversion.log 2>&1Several WordPress plugins can automatically generate and serve WebP/AVIF images:
- WebP Express: Converts images and works with the Nginx configuration
- EWWW Image Optimizer: Comprehensive image optimization
- Imagify: Easy WebP conversion
- ShortPixel Image Optimizer: Supports WebP and AVIF
For the best results:
- Install one of the above plugins
- Configure it to generate WebP/AVIF versions on upload
- Ensure the plugin saves the optimized versions with the correct file extension pattern (
.jpg.webp,.jpg.avif, etc.) - The Nginx configuration will automatically serve the optimized versions
- Always keep the original images as fallbacks
- Use appropriate quality settings (70-85) for a good balance of size and quality
- Test optimized images on different devices and browsers
- Consider using a CDN that supports content negotiation for WebP/AVIF
- Implement responsive images in your WordPress theme for additional optimization
Using WebP and AVIF formats can significantly improve website performance:
- WebP: 25-35% smaller than JPEG at similar quality
- AVIF: 50%+ smaller than JPEG at similar quality
- Faster load times lead to better SEO and user experience
- Reduced bandwidth consumption for both server and visitors