Skip to content

Instantly share code, notes, and snippets.

Embed
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.processor.register(ImageOptimProcessor)
c.job :optim do
process :optim
end
c.job :thumb do |geometry, format|
process :thumb, geometry
process :optim
encode format if format
end
end
class ImageOptimProcessor
def optim(temp_object)
io = ImageOptim.new
optimized = io.optimize_image(temp_object.path)
optimized.nil? ? temp_object : optimized
end
end
@teaforthecat

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.

@bobbus

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 : https://gist.github.com/bobbus/8913693

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.