Skip to content

Instantly share code, notes, and snippets.

@jdfdesign
Forked from scottatron/README.md
Created September 15, 2012 14:52
Show Gist options
  • Save jdfdesign/3728347 to your computer and use it in GitHub Desktop.
Save jdfdesign/3728347 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment