Class: Attached::Processor::Image

Inherits:
Base
  • Object
show all
Defined in:
lib/attached/processor/image.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#attachment, #file, #options

Instance Method Summary collapse

Methods inherited from Base

process

Constructor Details

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

Create a processor.

Parameters:

  • file - The file to be processed.

  • options - The options to be applied to the processing.

  • attachment - The attachment the processor is being run for.



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
# File 'lib/attached/processor/image.rb', line 26

def initialize(file, options = {}, attachment = nil)
  super

  @path      = self.file.path

  @size      = options[:size]
  @quality   = options[:quality]
  @extension = options[:extension]

  @width, @height, @operation = @size.match(/(\d*)x?(\d*)(.*)/)[1..3] if @size

  @width     ||= options[:width]
  @height    ||= options[:height]
  @operation ||= options[:operation]

  @operation = case @operation
  when :decrease then '>'
  when :increase then '<'
  when :default  then '#'
  else @operation || '#'
  end

  @extension ||= self.attachment.extension

  @width     = Integer(self.width)  if self.width
  @height    = Integer(self.height) if self.height
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



10
11
12
# File 'lib/attached/processor/image.rb', line 10

def extension
  @extension
end

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/attached/processor/image.rb', line 13

def height
  @height
end

#operationObject (readonly)

Returns the value of attribute operation.



15
16
17
# File 'lib/attached/processor/image.rb', line 15

def operation
  @operation
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/attached/processor/image.rb', line 9

def path
  @path
end

#qualityObject (readonly)

Returns the value of attribute quality.



14
15
16
# File 'lib/attached/processor/image.rb', line 14

def quality
  @quality
end

#widthObject (readonly)

Returns the value of attribute width.



12
13
14
# File 'lib/attached/processor/image.rb', line 12

def width
  @width
end

Instance Method Details

#processObject

Helper function for calling processors.

Usage:

self.process


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/attached/processor/image.rb', line 68

def process

  result = Tempfile.new(["", self.extension])
  result.binmode

  begin

    parameters = []

    parameters << self.path

    if width and height
      parameters << case operation
      when '#' then "-resize #{width}x#{height}^ -gravity center -extent #{width}x#{height}"
      when '<' then "-resize #{width}x#{height}\\<"
      when '>' then "-resize #{width}x#{height}\\>"
      else          "-resize #{width}x#{height}"
      end
    end

    parameters << "-quality #{quality}" if quality

    parameters << result.path

    parameters = parameters.join(" ").squeeze(" ")

    `convert #{parameters} #{redirect}`

    raise Errno::ENOENT if $?.exitstatus == 127

  rescue Errno::ENOENT
    raise "command 'convert' not found: ensure ImageMagick is installed"
  end

  unless $?.exitstatus == 0
    raise Attached::Processor::Error, "must be an image file"
  end

  return result

end

#redirectObject

Redirect output path.



57
58
59
# File 'lib/attached/processor/image.rb', line 57

def redirect
  ">/dev/null 2>&1" if File.exist?("/dev/null")
end