Class: Paperclip::Watermark

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Watermark.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/paperclip-watermark/watermark.rb', line 15

def initialize file, options = {}, attachment = nil
  super
  geometry          = options[:geometry]
  @file             = file
  if geometry.present?
    @crop             = geometry[-1,1] == '#'
  end
  @target_geometry  = Geometry.parse geometry
  @current_geometry = Geometry.from_file @file
  @convert_options  = options[:convert_options]
  @whiny            = options[:whiny].nil? ? true : options[:whiny]
  @format           = options[:format]
  @watermark_path   = options[:watermark_path]
  @position         = options[:position].nil? ? "SouthEast" : options[:position]
  @overlay          = options[:overlay].nil? ? true : false
  @current_format   = File.extname(@file.path)
  @basename         = File.basename(@file.path, @current_format)
end

Instance Attribute Details

#convert_optionsObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def convert_options
  @convert_options
end

#current_geometryObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def current_geometry
  @current_geometry
end

#formatObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def format
  @format
end

#overlayObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def overlay
  @overlay
end

#positionObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def position
  @position
end

#target_geometryObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def target_geometry
  @target_geometry
end

#watermark_pathObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def watermark_path
  @watermark_path
end

#whinyObject

Handles watermarking of images that are uploaded.



13
14
15
# File 'lib/paperclip-watermark/watermark.rb', line 13

def whiny
  @whiny
end

Instance Method Details

#convert_options?Boolean

Returns true if the image is meant to make use of additional convert options.

Returns:

  • (Boolean)


42
43
44
# File 'lib/paperclip-watermark/watermark.rb', line 42

def convert_options?
  not @convert_options.blank?
end

#crop?Boolean

Returns true if the target_geometry is meant to crop.

Returns:

  • (Boolean)


37
38
39
# File 'lib/paperclip-watermark/watermark.rb', line 37

def crop?
  @crop
end

#fromfileObject



76
77
78
# File 'lib/paperclip-watermark/watermark.rb', line 76

def fromfile
  File.expand_path(@file.path)
end

#makeObject

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/paperclip-watermark/watermark.rb', line 48

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

  command = "convert"
  params = [fromfile]
  params += transformation_command
  params << tofile(dst)
  begin
    success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
  rescue Paperclip::Errors::CommandNotFoundError
    raise Paperclip::Errors::CommandNotFoundError, "There was an error resizing and cropping #{@basename}" if @whiny
  end

  if watermark_path
    command = "composite"
    params = %W[-gravity #{@position} #{watermark_path} #{tofile(dst)}]
    params << tofile(dst)
    begin
      success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
    rescue Paperclip::Errors::CommandNotFoundError
      raise Paperclip::Errors::CommandNotFoundError, "There was an error processing the watermark for #{@basename}" if @whiny
    end
  end

  dst
end

#tofile(destination) ⇒ Object



80
81
82
# File 'lib/paperclip-watermark/watermark.rb', line 80

def tofile(destination)
  File.expand_path(destination.path)
end

#transformation_commandObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/paperclip-watermark/watermark.rb', line 84

def transformation_command
  if @target_geometry.present?
    scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
    trans = %W[-resize #{scale}]
    trans += %W[-crop #{crop} +repage] if crop
    trans << convert_options if convert_options?
    trans
  else
    scale, crop = @current_geometry.transformation_to(@current_geometry, crop?)
    trans = %W[-resize #{scale}]
    trans += %W[-crop #{crop} +repage] if crop
    trans << convert_options if convert_options?
    trans
  end
end