Skip to content

Instantly share code, notes, and snippets.

@scottatron
Created August 11, 2012 04:17
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scottatron/3320781 to your computer and use it in GitHub Desktop.
Save scottatron/3320781 to your computer and use it in GitHub Desktop.
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
Copy link

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
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