Class: Attached::Processor::Image
- Defined in:
- lib/attached/processor/image.rb
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(file, options = {}, attachment = nil) ⇒ Image
constructor
Create a processor.
-
#process ⇒ Object
Helper function for calling processors.
-
#redirect ⇒ Object
Redirect output path.
Methods inherited from Base
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, = {}, = nil) super @path = self.file.path @size = [:size] @quality = [:quality] @extension = [:extension] @width, @height, @operation = @size.match(/(\d*)x?(\d*)(.*)/)[1..3] if @size @width ||= [:width] @height ||= [:height] @operation ||= [:operation] @operation = case @operation when :decrease then '>' when :increase then '<' when :default then '#' else @operation || '#' end @extension ||= self..extension @width = Integer(self.width) if self.width @height = Integer(self.height) if self.height end |
Instance Attribute Details
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
10 11 12 |
# File 'lib/attached/processor/image.rb', line 10 def extension @extension end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
13 14 15 |
# File 'lib/attached/processor/image.rb', line 13 def height @height end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
15 16 17 |
# File 'lib/attached/processor/image.rb', line 15 def operation @operation end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/attached/processor/image.rb', line 9 def path @path end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
14 15 16 |
# File 'lib/attached/processor/image.rb', line 14 def quality @quality end |
#width ⇒ Object (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
#process ⇒ Object
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 |
#redirect ⇒ Object
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 |