Class: Paperclip::PapermillPaperclipProcessor

Inherits:
Thumbnail
  • Object
show all
Defined in:
lib/papermill/papermill_paperclip_processor.rb

Overview

Handles thumbnailing images that are uploaded.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PapermillPaperclipProcessor.


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/papermill/papermill_paperclip_processor.rb', line 10

def initialize(file, options = {}, attachment = nil)
  @crop_h, @crop_w, @crop_x, @crop_y = options[:crop_h], options[:crop_w], options[:crop_x], options[:crop_y]
  
  # copyright extraction
  if options[:geometry] =~ /©/ || options[:copyright]
    options[:geometry], *@copyright = options[:geometry].split("©", -1)
    
    @copyright = (
      (options[:copyright] != true && options[:copyright]) || 
      @copyright.join("©").nie || 
      file.instance.respond_to?(:copyright) && file.instance.copyright.nie || 
      file.instance.papermill_options[:copyright].nie)
    
    if @copyright
      @copyright = (options[:copyright_text_transform] || file.instance.papermill_options[:copyright_text_transform]).try(:call, @copyright) || @copyright
    end
  end
  
  # watermark extraction
  if options[:watermark] || options[:geometry] =~ /\-wm/
    options[:geometry] = options[:geometry].chomp("-wm")
    @watermark_path = options[:watermark].is_a?(String) && options[:watermark] || file.instance.papermill_options[:watermark]
    @watermark_path = "#{RAILS_ROOT}/public" + @watermark_path if @watermark_path.starts_with?("/")
  end
  
  
  if options[:geometry] =~ /#.+/
    # let's parse :
    # <width>x<height>#<crop_w>x<crop_h>:<crop_x>:<crop_y>
    # <width>x<height>#<crop_w>x<crop_h>
    # <width>x<height>#<crop_x>:<crop_y>
    
    options[:geometry], manual_crop = options[:geometry].split("#")
    crop_dimensions, @crop_x, @crop_y = manual_crop.split("+")
    
    if crop_dimensions =~ /x/
      @crop_w, @crop_h = crop_dimensions.split("x")
    else
      @crop_x, @crop_y = crop_dimensions, @crop_x
    end

    options[:geometry] = (options[:geometry].nie || "#{@crop_x}x#{@crop_y}") + "#"
    
    unless @crop_w && @crop_h
      @target_geometry = Geometry.parse(options[:geometry]) 
      @crop_w ||= @target_geometry.try(:width).to_i
      @crop_h ||= @target_geometry.try(:height).to_i
    end
  end
  super
end

Instance Attribute Details

Returns the value of attribute copyright


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def copyright
  @copyright
end

#crop_hObject

Returns the value of attribute crop_h


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def crop_h
  @crop_h
end

#crop_wObject

Returns the value of attribute crop_w


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def crop_w
  @crop_w
end

#crop_xObject

Returns the value of attribute crop_x


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def crop_x
  @crop_x
end

#crop_yObject

Returns the value of attribute crop_y


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def crop_y
  @crop_y
end

#watermark_pathObject

Returns the value of attribute watermark_path


8
9
10
# File 'lib/papermill/papermill_paperclip_processor.rb', line 8

def watermark_path
  @watermark_path
end

Instance Method Details


66
67
68
# File 'lib/papermill/papermill_paperclip_processor.rb', line 66

def copyright_command
  (options[:copyright_im_command] || @file.instance.papermill_options[:copyright_im_command]).gsub(/%s/, @copyright.gsub(/'/, %{'"'"'}).sub("-slash-", "/").sub("-backslash-", %{\\\\\\})) if @copyright
end

#crop_commandObject


74
75
76
77
78
# File 'lib/papermill/papermill_paperclip_processor.rb', line 74

def crop_command
  if @crop_h || @crop_x
    " -crop '%dx%d+%d+%d'" % [ @crop_w, @crop_h, @crop_x, @crop_y ].map(&:to_i)
  end
end

#transformation_commandObject


62
63
64
# File 'lib/papermill/papermill_paperclip_processor.rb', line 62

def transformation_command
  " -limit memory 1 -limit map 1 #{(crop_command ? super.sub(/ -crop \S+/, crop_command) : super)} #{copyright_command} #{watermark_command}".sub(%{-resize "0x" }, "")
end

#watermark_commandObject


70
71
72
# File 'lib/papermill/papermill_paperclip_processor.rb', line 70

def watermark_command
  (options[:watermark_im_command] || @file.instance.papermill_options[:watermark_im_command]).gsub(/%s/, @watermark_path) if @watermark_path
end