Skip to content

Instantly share code, notes, and snippets.

@bobbus
Forked from scottatron/README.md
Last active April 13, 2017 16:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bobbus/8913693 to your computer and use it in GitHub Desktop.
Save bobbus/8913693 to your computer and use it in GitHub Desktop.
Dragonfly and ImageOptim setup

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
@sachin87
Copy link

could you show me your complete dragonfly.rb code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment