Class: Milton::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/milton/derivatives/thumbnail/image.rb

Overview

Generic view of an “Image”, or rather, something with a width and a height we care about =).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = nil, height = nil) ⇒ Image

Instantiates a new Image with the given width and height



29
30
31
32
# File 'lib/milton/derivatives/thumbnail/image.rb', line 29

def initialize(width=nil, height=nil)
  @width  = width.to_i
  @height = height.to_i
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



6
7
8
# File 'lib/milton/derivatives/thumbnail/image.rb', line 6

def height
  @height
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/milton/derivatives/thumbnail/image.rb', line 5

def width
  @width
end

Class Method Details

.from_geometry(geometry) ⇒ Object

Instantiates a new image from the given geometry string. A geometry string is just something like 50x40. The first number is the width and the second is the height.



23
24
25
# File 'lib/milton/derivatives/thumbnail/image.rb', line 23

def from_geometry(geometry)
  new(*(geometry.split("x").collect(&:to_i)))
end

.from_path(path) ⇒ Object

Instantiates a new image from the given path. Uses ImageMagick’s identify method to determine the width and height of the image with the given path and returns a new Image with those dimensions.

Raises a MissingFileError if the given path could not be identify’d by ImageMagick (resulting in a height and width).



15
16
17
18
# File 'lib/milton/derivatives/thumbnail/image.rb', line 15

def from_path(path)
  raise Milton::MissingFileError.new("Could not identify #{path} as an image, does the file exist?") unless Milton.syscall("identify #{path}") =~ /.*? (\d+)x(\d+)\+\d+\+\d+/
  new($1, $2)
end

Instance Method Details

#larger_dimensionObject

Returns the larger dimension of the Image



35
36
37
# File 'lib/milton/derivatives/thumbnail/image.rb', line 35

def larger_dimension
  width > height ? width : height
end

#square?Boolean

Returns true if the Image is square

Returns:

  • (Boolean)


45
46
47
# File 'lib/milton/derivatives/thumbnail/image.rb', line 45

def square?
  width == height
end

#wider?Boolean

Returns true if the Image is wider than it is tall

Returns:

  • (Boolean)


40
41
42
# File 'lib/milton/derivatives/thumbnail/image.rb', line 40

def wider?
  width > height
end