Reducing Image Sizes For Entire Blog

Reducing Image Sizes For Entire Blog

I am lazy and when I add photos to this blog I do not do ANY resizing or adjustments.  

Well, over time, I have realized that having multiple multi-Megabyte size images on a page is not great for load speeds.  So I looked for a way to reduce all of my image sizes.

Luckily Ghost does a good job of putting all the images in <install-dir>/content/images/<year>/<month>.  Also luckily, this AskUbuntu question gave  me a simple solution.

The ImageMagick utility includes a convert tool to resize images. I found that this also creates a copy of the original which was not what I needed for my purposes.  I wanted to edit the images in place.  A little further down and the solution presented itself like a nice present...the mogrify utility would resize images in place.

So I tried this out on a recent post and everything seems to work as advertised.

sudo mogrify -resize 50% *.jpg

The first major thing to make note of is that some cameras/phones save the extension in caps: .jpg versus .JPG so be careful with that if scripting this.

The second thing was to figure out how to resize all images to a specific file size instead of a percentage.  More searching, more answers in the form of the -define jpeg:extent=<size> option.  Though this turns out to also go for maximum compression which is not what I want. I finally settled on adjusting the quality of the image using the -quality option which takes a percent quality to adjust to.  After some playing around I settled on 25%, as I started noticing actual degradation any lower than that. I certainly could go lower quality just for the purposes of faster page loads, but let's be honest, no one is viewing this blog because of the blazing loads.

Lastly was to script this out and run the resize against all of my pictures. Find all pictures (not any directories) that have a size greater than 1 MB and try to compress them.

find ./content/images/ -type f -size +1M -iname *.jpg -exec sudo -u ghost mogrify -quality 25 {} \;

My test run for my last post shows that almost every picture is going to end up MUCH smaller (like around in the mid to high hundreds of KBs). The largest image at 2.7MB was resized down to 1.1MB without any major quality difference, So I may go back and do this to everything.