Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Lossless compression of images in Rails using Dragonfly & image_optim

This runs provides an .optim job for dragonfly and also adds the .optim job into .thumb

This requires you have the 'image_optim' gem and it's dependencies.

require 'dragonfly/image_optim_processor'
app = Dragonfly[:images]
app.configure do |c|
c.job :optim do
process :optim
c.job :thumb do |geometry, format|
process :thumb, geometry
process :optim
encode format if format
class ImageOptimProcessor
def optim(temp_object)
io =
optimized = io.optimize_image(temp_object.path)
optimized.nil? ? temp_object : optimized

This comment has been minimized.

Copy link

teaforthecat commented Jan 14, 2013

Very helpful gist! I'm wondering why you have optim before encode?
I would assume that optim would be the last thing to be done.


This comment has been minimized.

Copy link

bobbus commented Feb 10, 2014

I had to tweak it a bit for last version of ImageOptim and Dragonfly, see :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.