Class: HasImage::Processor
- Inherits:
-
Object
- Object
- HasImage::Processor
- Defined in:
- lib/has_image/processor.rb
Overview
Image processing functionality for the HasImage gem.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.geometry_string_valid?(string) ⇒ Boolean
“The form of an extended geometry string is <width>x<height>+-<xoffset>+-<yoffset>%!<>”.
-
.valid?(arg) ⇒ Boolean
Arg should be either a file, or a path.
Instance Method Summary collapse
-
#initialize(options) ⇒ Processor
constructor
The constuctor should be invoked with the options set by has_image.
-
#resize(file, size) ⇒ Object
Create the resized image, and transforms it to the desired output format if necessary.
-
#resize_image(size) ⇒ Object
Image resizing is placed in a separate method for easy monkey-patching.
Constructor Details
#initialize(options) ⇒ Processor
The constuctor should be invoked with the options set by has_image.
33 34 35 |
# File 'lib/has_image/processor.rb', line 33 def initialize() # :nodoc: @options = end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/has_image/processor.rb', line 8 def @options end |
Class Method Details
.geometry_string_valid?(string) ⇒ Boolean
“The form of an extended geometry string is <width>x<height>+-<xoffset>+-<yoffset>%!<>”
15 16 17 |
# File 'lib/has_image/processor.rb', line 15 def geometry_string_valid?(string) string =~ /\A[\d]*x[\d]*([+-][0-9][+-][0-9])?[%@!<>^]?\Z/ end |
.valid?(arg) ⇒ Boolean
Arg should be either a file, or a path. This runs ImageMagick’s “identify” command and looks for an exit status indicating an error. If there is no error, then ImageMagick has identified the file as something it can work with and it will be converted to the desired output format.
23 24 25 26 27 28 29 |
# File 'lib/has_image/processor.rb', line 23 def valid?(arg) arg.close if arg.respond_to?(:close) && !arg.closed? silence_stderr do `identify #{arg.respond_to?(:path) ? arg.path : arg.to_s}` $? == 0 end end |
Instance Method Details
#resize(file, size) ⇒ Object
Create the resized image, and transforms it to the desired output format if necessary. The size should be a valid ImageMagick geometry string.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/has_image/processor.rb', line 40 def resize(file, size) unless Processor.geometry_string_valid?(size) raise InvalidGeometryError.new('"%s" is not a valid ImageMagick geometry string' % size) end silence_stderr do path = file.respond_to?(:path) ? file.path : file file.close if file.respond_to?(:close) && !file.closed? @image = MiniMagick::Image.from_file(path) convert_image resize_image(size) return @image end rescue MiniMagick::MiniMagickError raise ProcessorError.new("That doesn't look like an image file.") end |
#resize_image(size) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/has_image/processor.rb', line 65 def resize_image(size) @image. do |commands| commands.send("auto-orient".to_sym) commands.strip # Fixed-dimension images if size =~ /\A[\d]*x[\d]*!?\Z/ commands.resize "#{size}^" commands.gravity "center" commands.extent size # Non-fixed-dimension images else commands.resize "#{size}" end commands.quality [:output_quality] end end |