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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/attached/processor/image.rb', line 21

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.



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

def extension
  @extension
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#operationObject (readonly)

Returns the value of attribute operation.



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

def operation
  @operation
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/attached/processor/image.rb', line 7

def path
  @path
end

#qualityObject (readonly)

Returns the value of attribute quality.



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

def quality
  @quality
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#processObject

Helper function for calling processors.

Usage:

self.process


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/attached/processor/image.rb', line 52

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.



43
44
45
# File 'lib/attached/processor/image.rb', line 43

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