Class: HasImage::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/has_image/processor.rb

Overview

Image processing functionality for the HasImage gem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options) # :nodoc:
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.geometry_string_valid?(string) ⇒ Boolean

“The form of an extended geometry string is <width>x<height>+-<xoffset>+-<yoffset>%!<>”

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

Image resizing is placed in a separate method for easy monkey-patching. This is intended to be invoked from resize, rather than directly. By default, the following ImageMagick functionality is invoked:



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.combine_options 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 options[:output_quality]
  end
end