Class: Paperclip::Watermark

Inherits:
Processor
  • Object
show all
Defined in:
lib/paperclip_processors/watermark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Watermark

Returns a new instance of Watermark.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paperclip_processors/watermark.rb', line 11

def initialize file, options = {}, attachment = nil
  super
  @file             = file
  @whiny            = options[:whiny].nil? ? true : options[:whiny]
  @format           = options[:format]
  @watermark_path   = options[:watermark_path]
  @position         = options[:watermark_position].nil? ? "SouthEast" : options[:watermark_position]
  
  @current_format   = File.extname(@file.path)
  @basename         = File.basename(@file.path, @current_format)
end

Instance Attribute Details

#formatObject

Handles watermarking of images that are uploaded.



9
10
11
# File 'lib/paperclip_processors/watermark.rb', line 9

def format
  @format
end

#positionObject

Handles watermarking of images that are uploaded.



9
10
11
# File 'lib/paperclip_processors/watermark.rb', line 9

def position
  @position
end

#watermark_pathObject

Handles watermarking of images that are uploaded.



9
10
11
# File 'lib/paperclip_processors/watermark.rb', line 9

def watermark_path
  @watermark_path
end

#whinyObject

Handles watermarking of images that are uploaded.



9
10
11
# File 'lib/paperclip_processors/watermark.rb', line 9

def whiny
  @whiny
end

Instance Method Details

#fromfileObject



43
44
45
# File 'lib/paperclip_processors/watermark.rb', line 43

def fromfile
  "\"#{ File.expand_path(@file.path) }[0]\""
end

#makeObject

Performs the conversion of the file into a watermark. Returns the Tempfile that contains the new image.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/paperclip_processors/watermark.rb', line 25

def make
  return @file unless watermark_path
  
  dst = Tempfile.new([@basename, @format].compact.join("."))
  dst.binmode

  command = "composite"
  params = "-gravity #{@position} #{watermark_path} #{fromfile} #{tofile(dst)}"

  begin
    success = Paperclip.run(command, params)
  rescue PaperclipCommandLineError
    raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
  end

  dst
end

#tofile(destination) ⇒ Object



47
48
49
# File 'lib/paperclip_processors/watermark.rb', line 47

def tofile(destination)
  "\"#{ File.expand_path(destination.path) }[0]\""
end