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.
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, = {}, = 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.
8 9 10 |
# File 'lib/attached/processor/image.rb', line 8 def extension @extension end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/attached/processor/image.rb', line 10 def height @height end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
12 13 14 |
# File 'lib/attached/processor/image.rb', line 12 def operation @operation end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/attached/processor/image.rb', line 7 def path @path end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
11 12 13 |
# File 'lib/attached/processor/image.rb', line 11 def quality @quality end |
#width ⇒ Object (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
#process ⇒ Object
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 |
#redirect ⇒ Object
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 |