One of the ways to speed up your site and save bandwidth is to optimize your images. You can often halve an image’s size without losing (much) quality. Many photo editors may give you this option, but it’s a slow manual process. What we really need is a way to automate this so that every time we deploy changes we can optimize in one step.

Getting Started

You’ll need 2 tools (and Cygwin if you’re on Windows) to pull this off:

You can do a LOT with ImageMagick. Here’s a command-line reference for it.

Compressing images the Groovy way

Here is a Groovy script I wrote for that purpose. It would be even cooler to use something like this in a Gant build:

// If directory not passed use the current directory
def dir = args?.size() ? args[0] : "."
// 70 is a fairly safe value for quality
def quality = args?.size() > 1 ? args[1] : 70

// Get an ArrayList of all PNG and JPG files
images = new File(dir).listFiles().grep(~/.*(png|jpg)$/)

for (img in images) {
    print "Crushing ${img} - size: ${img.size()}-> "
    // Compress the image
    "convert -quality ${quality} ${img} ${img}".execute().text
    println "${img.size()}"

Tweak this to your liking to suit your needs. You can substitute your favorite command-line image editor if you like. Groovy makes it easy!

Run it by typing this on your command-line:

groovy src/groovy/CompressImages images/ 60

What do you think? Useful? Share!

Posted on .