WordPress Image Optimization

Something I’ve tried to do, then forgot I was trying to do, for while now, is to optimize the images across the WordPress sites I have.   It’s actually just this one for me, and my wife’s two old blogs she no longer really uses.  There are a variety of plug ins that do this, I believe all of them require subscribing to some sort of API.  I’m not spending \$100 to optimize my 22,000 images and I’m not going to remember to come back monthly to chip away at 500 free images per month.

The thing is, I’m also, pretty good with the back end Linux, and while I’m not sure of the best tools out there, I am sure there are tools to compress and resize images.  I also know there are plenty of ways to do automated processing of large number of documents using Bash scripts.  I do it all the time.

Enter, the trusty AI coding companion.

I would like to make a shell script that will optimize my image library on my WordPress site.

It shpuld recursively go through all subdirectories from the directory its run in and process anybimages, JPG, PNG, GIF.

If possible it should compress the images for savings, if nothing will really change, it should skip the image.

Any image wider or taller than 2048, should be resized to be at most 2048 on its lenth or width, keeping the original aspect ration.

I would also like to see some statistics as output. Maybe, folder size before and after, how many images were compressed, how much file system was freed up etc.

It should also output to a seperate file (seperate from the statistics) a list of any images with errors (path and file name).  

Simple, straight forward, easy. followed up with a Readme request for any install requirements.

I made a local copy backup of the images folder, then tested it out. The problem was, it could see the images, but wasn’t actually processing them. After feeding the results back, Claude got it working.

The first run, on the 2024 images only

Finished reading 3540 files from list
========================================
OPTIMIZATION COMPLETE
========================================
Images Found:        3540
Images Processed:    1404
Images Resized:      310
Images Skipped:      2136
Errors:              0
Size Before:         810.04MB
Size After:          599.52MB
Total Saved:         210.52MB (25%)
========================================

Which is pretty good.

I ran is across the entire uploads folder next.

Finished reading 24469 files from list
========================================
OPTIMIZATION COMPLETE
========================================
Images Found:        24469
Images Processed:    7770
Images Resized:      1448
Images Skipped:      16699
Errors:              0
Size Before:         4.38GB
Size After:          2.90GB
Total Saved:         1.49GB (33%)
========================================

So a few BG saves, load times will be overall better. I had to let it run overnight but it’s working great. Maybe it’s not the ‘Most optimized” but it’s gree a got the job done enough.

The complete script is here, in my AI Code Repository.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.