Image Optimization for Google PageSpeed
Shamil Dashapylov, CEO
🚀 Website Image Optimization - Simple and Effective
Hi! Today we are sharing a practical way to optimize images on your server so your website loads faster and gets better Google PageSpeed scores.
🔧 What you need
Install these two utilities on your VPS:
- jpegoptim - for JPEG/JPG optimization
- optipng - for PNG optimization
On Ubuntu and Debian, run:
sudo apt install jpegoptim optipng
⚙️ Optimization commands
Go to your website root directory and run:
find . -type f \( -name "*.jpeg" -or -name "*.jpg" \) -exec jpegoptim --strip-all {} \;
find . -type f -name "*.png" -exec optipng -strip all {} \;
What these commands do:
- 🔍 The first command finds all
.jpegand.jpgfiles and optimizes them withjpegoptim. - 🔍 The second command finds all
.pngfiles and optimizes them withoptipng.
Images are cleaned from:
- EXIF data (camera details, location, etc.)
- extra tags
- metadata
⚡ Visual quality remains almost unchanged while file size goes down.
These command parameters are chosen for fast execution, solid compression, and compliance with Google PageSpeed recommendations.
✅ At this point, Google PageSpeed can rate these optimized images at 100%.
🖼️ What about newly uploaded images?
If you upload images through a content editor, we recommend JCE Editor.
It includes automatic EXIF cleanup for JPEG and PNG files during upload.
This will not replace full optimization, but it still improves image quality and loading performance.
🎯 Result
Server-side image optimization helps you:
- Improve website loading speed
- Increase Google PageSpeed scores
- Reduce hosting load and traffic usage
💡 Want your website to run fast and stay stable? The Aspire Agency team can help with optimization, setup, and performance improvements.
Bonus: ⏰ Cron automation
1. Create an image optimization script
Create a file, for example:
/opt/image-optimize.sh
File content:
#!/bin/bash
# Path to your website root
SITE_PATH="/var/www/html" # Replace with your path
# Log file path
LOG_FILE="/var/log/image-optimize.log"
echo "===== Optimization started: $(date) =====" >> $LOG_FILE
# JPEG optimization
find $SITE_PATH -type f \( -name "*.jpeg" -or -name "*.jpg" \) -exec jpegoptim --strip-all {} \; >> $LOG_FILE 2>&1
# PNG optimization
find $SITE_PATH -type f -name "*.png" -exec optipng -strip all {} \; >> $LOG_FILE 2>&1
echo "===== Optimization finished: $(date) =====" >> $LOG_FILE
echo "" >> $LOG_FILE
✅ Make it executable:
sudo chmod +x /opt/image-optimize.sh
2. Add it to cron
Run:
crontab -e
Add this line to run every day at 3:00 AM:
0 3 * * * /opt/image-optimize.sh
📌 Cron expression breakdown:
0 3 * * * - every day at 03:00
Other scheduling options:
- Weekly on Sunday at 2:00 AM:
0 2 * * 0 /opt/image-optimize.sh
- Every 6 hours:
0 */6 * * * /opt/image-optimize.sh
Logs
Script logs are saved to:
/var/log/image-optimize.log
View logs:
cat /var/log/image-optimize.log
Watch latest lines in real time:
tail -f /var/log/image-optimize.log
🔥 Done
Your images will now be optimized automatically without manual work. Your site will stay faster, and Google PageSpeed results should improve.
Best regards,
ASPIRE Team