How to Compress Images with the ImageOptimization RubyGem

Feature thumb ruby image compression tutorial

Image compression can make a significant difference in the performance of your applications. There are a number of online utilities that can compress images, but this guide shows how to compress them on your own system using the ImageOptimizer RubyGem. As a side note, this guide's featured image was used with this exact technique and was compressed by 87%.

(This guide assumes you're on a Mac)

In order to use the gem, you first need to install a few dependencies, you can do this using Homebrew with the command:

brew install optipng jpegoptim gifsicle pngquant

Once you have those installed, you can install the Gem. You can install it in your Gemfile if this is going in a Ruby project, or you can simply run it from the command line to compress images on your local system. To install on your local system, run the command:

gem install image_optimizer

After that's installed, you can open an irb session and compress images with commands such as and get compression rates as high as you need:

Below is a link to the full documentation and additional image optimization examples for file types such as png