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.



41
42
43
# File 'lib/has_image/processor.rb', line 41

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.nil? || 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.

Note that this is used in place of any mimetype checks. HasImage assumes that is ImageMagick is able to process the file, then it’s OK. Since ImageMagick can be compiled with many different options for supporting various file types (or not) this is safer and more complete than checking the mime type.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/has_image/processor.rb', line 30

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

#measure(path, dimension) ⇒ Object

Gets the given dimension (width/height) from the image file at path.



65
66
67
# File 'lib/has_image/processor.rb', line 65

def measure(path, dimension)
  MiniMagick::Image.from_file(path)[dimension.to_sym]
end

#process(file, size = options[:resize_to], format = options[:convert_to]) ⇒ Object Also known as: resize

Creates the resized image, and transforms it to the desired output format if necessary.

size should be a valid ImageMagick geometry string. format should be an image format supported by ImageMagick, e.g. “PNG”, “JPEG” If a block is given, it yields the processed image file as a file-like object using IO#read.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/has_image/processor.rb', line 51

def process(file, size = options[:resize_to], format = options[:convert_to])
  unless Processor.geometry_string_valid?(size)
    raise InvalidGeometryError.new('"%s" is not a valid ImageMagick geometry string' % size)
  end
  with_image(file) do |image|
    convert_image(image, format) if format
    resize_image(image, size) if size
    yield IO.read(image.path) if block_given?
    image
  end
end