public
Last active

Paperclip Watermark processor

  • Download Gist
watermark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class User
has_attached_file :photo,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/watermark.png"
},
:thumb => "100x100>",
}
end
 
# Rails.root /lib/paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@watermark_path = options[:watermark_path]
@position = options[:position].nil? ? "SouthEast" : options[:position]
end
 
def make
src = @file
dst = Tempfile.new([@basename].compact.join("."))
dst.binmode
 
return super unless @watermark_path
 
params = "-gravity #{@position} #{transformation_command.join(" ")} #{@watermark_path} :source :dest"
 
begin
success = Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
end
 
dst
end
end
end

Thanks It worked for me, after loading the file manually on the model:

class Photo < ActiveRecord::Base
require 'paperclip_processors/watermark'
....

Thanks so much!

I keep getting an error:

NameError (uninitialized constant Paperclip::Watermark::PaperclipCommandLineError):
lib/paperclip/watermark.rb:66:in rescue in make'
lib/paperclip/watermark.rb:64:in
make'
app/controllers/assets_controller.rb:10:in `create'

It seems like some of your library is missing.

Allow me couple of minutes before we engage on the meeting.

On 8/2/12 2:34 AM, "Kevin Collignon"
.com> wrote:

I keep getting an error:

NameError (uninitialized constant
Paperclip::Watermark::PaperclipCommandLineError):
lib/paperclip/watermark.rb:66:in rescue in make'
lib/paperclip/watermark.rb:64:in
make'
app/controllers/assets_controller.rb:10:in `create'


Reply to this email directly or view it on GitHub:
https://gist.github.com/784445

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.