Skip to content

Instantly share code, notes, and snippets.

@chrise86
Forked from bobbus/README.md
Created April 29, 2014 13:49
Show Gist options
  • Save chrise86/11400962 to your computer and use it in GitHub Desktop.
Save chrise86/11400962 to your computer and use it in GitHub Desktop.

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.

class OptimizedThumb < Dragonfly::ImageMagick::Processors::Thumb
def call(*args, &block)
super
args[0].process!(:optim)
end
end
Dragonfly.app.configure do
### Image Optim processor
processor :optim do |content|
if optimized = ImageOptim.new.optimize_image(content.path).presence
# We have to use a Tempfile which copy the optimized image because optimized correspond to a Tempfile path which may be deleted
tempfile = Tempfile.new('optimized-image')
begin
optimized.copy(tempfile.path)
content.update tempfile
ensure
tempfile.close # here we cannot use `close!` because of https://github.com/markevans/dragonfly/issues/319
end
end
end
processor :thumb, OptimizedThumb.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment