Skip to content

Instantly share code, notes, and snippets.

@amiel
Created May 22, 2012 20:51
Show Gist options
  • Save amiel/2771555 to your computer and use it in GitHub Desktop.
Save amiel/2771555 to your computer and use it in GitHub Desktop.
Simple watermarking with Thumbkit and CarrierWave
class MyUploader < CarrierWave::Uploader::Base
include Thumbkit::Adapters::CarrierWave
# Watermark should always be processed after thumbkit, ensuring that we always
# have a valid image and we don't need to change the extension
def watermark(watermark_image, options = {})
cache_stored_file! if !cached?
Watermarker.new(current_path, watermark_image).watermark!(options)
end
version :preview, :if => :is_image do
process :thumbkit => [200, 200]
process :watermark => [Rails.root.join('app/assets/images/watermark.gif')]
def full_filename(for_file = model.file.file)
[version_name, thumbkit_filename(for_file)].join('_')
end
end
require 'mini_magick'
class Watermarker
def initialize(original_path, watermark_path)
@original_path = original_path.to_s
@watermark_path = watermark_path.to_s
end
# Accepted options: :gravity
#
# Gravity options: None, Center, East, Forget, NorthEast, North, NorthWest,
# SouthEast, South, SouthWest, West, Static
def watermark!(options = {})
options[:gravity] ||= 'center'
image = MiniMagick::Image.open(@original_path)
result = image.composite(MiniMagick::Image.open(@watermark_path)) do |c|
c.gravity options[:gravity]
end
result.write @original_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment