Class: Imageproxy::Convert

Inherits:
Command
  • Object
show all
Defined in:
lib/imageproxy/convert.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, settings = {}) ⇒ Convert

Returns a new instance of Convert.



8
9
10
11
12
13
14
15
16
# File 'lib/imageproxy/convert.rb', line 8

def initialize(options, settings={})
  @options = options
  @settings = settings

  if (!(options.resize || options.thumbnail || options.rotate || options.flip || options.format ||
    options.quality || options.overlay))
    raise "Missing action or illegal parameter value"
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/imageproxy/convert.rb', line 6

def options
  @options
end

Instance Method Details

#convert_optionsObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/imageproxy/convert.rb', line 39

def convert_options
  convert_options = []
  convert_options << "-resize #{resize_thumbnail_options(options.resize)}" if options.resize
  convert_options << "-thumbnail #{resize_thumbnail_options(options.thumbnail)}" if options.thumbnail
  convert_options << "-flop" if options.flip == "horizontal"
  convert_options << "-flip" if options.flip == "vertical"
  convert_options << rotate_options if options.rotate
  convert_options << "-colors 256" if options.format == "png8"
  convert_options << "-quality #{options.quality}" if options.quality
  convert_options << interlace_options if options.progressive
  convert_options.join " "
end

#execute(user_agent = nil, timeout = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/imageproxy/convert.rb', line 18

def execute(user_agent=nil, timeout=nil)
  if options.overlay
    @overlay_file ||= Tempfile.new("imageproxy").tap(&:close)
    try_command_with_timeout(curl options.overlay, :user_agent => user_agent, :timeout => timeout, :output => @overlay_file.path)
    try_command_with_timeout curl(options.source, :user_agent => user_agent, :timeout => timeout) +
                      "| composite #{@overlay_file.path} - - | convert - #{convert_options} #{new_format}#{file.path}"
    file
  else
    try_command_with_timeout %'#{curl options.source, :user_agent => user_agent, :timeout => timeout} | convert - #{convert_options} #{new_format}#{file.path}'
    file
  end
end

#fileObject



90
91
92
93
94
95
96
97
# File 'lib/imageproxy/convert.rb', line 90

def file
  @tempfile ||= begin
    file = Tempfile.new("imageproxy")
    file.chmod 0644 if @settings[:world_readable_tempfile]
    file.close
    file
  end
end

#interlace_optionsObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/imageproxy/convert.rb', line 75

def interlace_options
  case options.progressive
    when "true"
      "-interlace JPEG"
    when "false"
      "-interlace none"
    else
      ""
  end
end

#new_formatObject



86
87
88
# File 'lib/imageproxy/convert.rb', line 86

def new_format
  options.format ? "#{options.format}:" : ""
end

#resize_thumbnail_options(size) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/imageproxy/convert.rb', line 52

def resize_thumbnail_options(size)
  case options.shape
    when "cut"
      "#{size}^ -gravity center -extent #{size}"
    when "preserve"
      size
    when "pad"
      background = options.background ? %|"#{options.background}"| : %|none -matte|
      "#{size} -background #{background} -gravity center -extent #{size}"
    else
      size
  end
end

#rotate_optionsObject



66
67
68
69
70
71
72
73
# File 'lib/imageproxy/convert.rb', line 66

def rotate_options
  if options.rotate.to_f % 90 == 0
    "-rotate #{options.rotate}"
  else
    background = options.background ? %|"#{options.background}"| : %|none|
    "-background #{background} -matte -rotate #{options.rotate}"
  end
end

#try_command_with_timeout(cmd) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/imageproxy/convert.rb', line 31

def try_command_with_timeout cmd
  Timeout::timeout(10) { execute_command cmd }
rescue Timeout::Error => e
  puts "Command timed out after 10 seconds, retrying >#{cmd}<"
  execute_command cmd
  puts "SUCCESS " * 20
end