-
-
Save soulcutter/3dabbaf6bd9dadaa235a to your computer and use it in GitHub Desktop.
Dragonfly gem Watermark processor blog snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
photograph.original.thumb('640x480>').watermark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
photograph.original.thumb('640x480>') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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