Usage:
#fill_watermark_text(img, width, height, text, gravity = "Center")
attachment_url(object, :attachment, :fill_watermark_text, 800, 600, "My Watermark")
Usage:
#fill_watermark_text(img, width, height, text, gravity = "Center")
attachment_url(object, :attachment, :fill_watermark_text, 800, 600, "My Watermark")
# config/initializers/refile.rb | |
require "refile/watermark" |
# lib/refile/watermark.rb | |
require "refile" | |
require "mini_magick" | |
module Refile | |
# Adds watermarking to Refile | |
class Watermark | |
# @param [Symbol] method The method to invoke on {#call} | |
def initialize(method) | |
@method = method | |
end | |
# Watermarks the image with another image, and also uses the fill processor | |
# to resize the initial image | |
# | |
# The resulting image will always be exactly as large as the specified | |
# dimensions. | |
# | |
# By default, the watermark and original image will be placed in the center but this can be | |
# changed via the `gravity` option. | |
# | |
# @param [MiniMagick::image] img the background image which will be modified | |
# @param [#to_s] width the width to fill out | |
# @param [#to_s] height the height to fill out | |
# @param [string] watermark_image_filename the image to use as watermark (file must be in the app/assets/images folder) | |
# @param [string] gravity which part of the image to focus on and put the watermark on | |
# @return [void] | |
# @see http://www.imagemagick.org/script/command-line-options.php#gravity | |
def fill_watermark_image(img, width, height, watermark_image_filename, gravity = "Center") | |
Refile::MiniMagick.new(:fill).fill(img, width, height, gravity) | |
second_image = ::MiniMagick::Image.new(Rails.root.join('app', 'assets', 'images', watermark_image_filename).to_s) | |
result = img.composite(second_image) do |composite| | |
composite.compose "Over" # OverCompositeOp | |
#composite.geometry "+20+20" # copy second_image onto first_image from (20, 20) | |
composite.dissolve "20,100" # make second_image 50% transparent on top of first image | |
composite.gravity gravity | |
end | |
result.write img.path | |
end | |
# Watermarks the image with text, and also uses the fill processor | |
# to resize the initial image | |
# | |
# The resulting image will always be exactly as large as the specified | |
# dimensions. | |
# | |
# By default, the original image will be placed in the center. | |
# The watermark will always be on the middle-right. | |
# | |
# @param [MiniMagick::image] img the background image which will be modified | |
# @param [#to_s] width the width to fill out | |
# @param [#to_s] height the height to fill out | |
# @param [string] watermark_image_filename the image to use as watermark (file must be in the app/assets/images folder) | |
# @param [string] gravity which part of the image to focus on | |
# @return [void] | |
# @see http://www.imagemagick.org/script/command-line-options.php#gravity | |
def fill_watermark_text(img, width, height, text, gravity = "Center") | |
Refile::MiniMagick.new(:fill).fill(img, width, height, gravity) | |
boxheight = (height.to_i*0.8).round(2) - (height.to_i*0.4).round(2) | |
fontsize = (boxheight / 4) | |
img.combine_options do |c| | |
c.draw "fill #cccccc fill-opacity 0.4 roundrectangle #{(width.to_i*0.6).round(2)},#{(height.to_i*0.4).round(2)} #{width},#{(height.to_i*0.8).round(2)} 10,10" | |
c.pointsize fontsize | |
c.draw "fill #000000 fill-opacity 1 text #{(width.to_i*0.6+10).round(2)},#{(height.to_i*0.8-(boxheight/8)).round(2)} \"#{text}\"" | |
end | |
end | |
# Process the given file. The file will be processed via one of the | |
# instance methods of this class, depending on the `method` argument passed | |
# to the constructor on initialization. | |
# | |
# If the format is given it will convert the image to the given file format. | |
# | |
# @param [Tempfile] file the file to manipulate | |
# @param [String] format the file format to convert to | |
# @return [File] the processed file | |
def call(file, *args, format: nil, &block) | |
img = ::MiniMagick::Image.new(file.path) | |
img.format(format.to_s.downcase, nil) if format | |
send(@method, img, *args, &block) | |
::File.open(img.path, "rb") | |
end | |
end | |
end | |
# Register Watermark as a valid Refile processor | |
[:fill_watermark_image, :fill_watermark_text].each do |name| | |
Refile.processor(name, Refile::Watermark.new(name)) | |
end |