Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created March 18, 2013 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulcutter/3dabbaf6bd9dadaa235a to your computer and use it in GitHub Desktop.
Save soulcutter/3dabbaf6bd9dadaa235a to your computer and use it in GitHub Desktop.
Dragonfly gem Watermark processor blog snippets
photograph.original.thumb('640x480>').watermark
photograph.original.thumb('640x480>')
# ... other initialization stuff above here ...
Dragonfly[:images].processor.register(Watermark)
# optional, but gives you the ability to use watermark(opacity: 50)
# rather than process(:watermark, opacity: 50)
Dragonfly[:images].configure do |c|
c.job(:watermark) { |*args| process(:watermark, *args) }
end
class Watermark
include Configurable
include ImageMagick::Utils
def watermark(source_image, opts = {})
opts = defaults.merge(opts)
# uses the source image dimensions to resize the watermark with
image_properties = identify(source_image)
watermark_resize = "#{image_properties[:width]}x#{image_properties[:height]}^"
# assembles an ImageMagick convert command which does all the work
convert(
source_image,
"( #{opts[:watermark]} -resize #{watermark_resize} ) -compose dissolve -define compose:args=#{opts[:opacity]} -composite"
)
# as you can see the convert syntax is somewhat inscrutable, which is why
# it's called ImageMagick and not ImageObvious
end
def defaults
{
opacity: 60, # 0-100 where 0 is opaque
watermark: File.new("watermark-logo.png").path # a path to a default watermark image
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment