Class: Paperclip::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/geometry.rb

Overview

Defines the geometry of an image.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = nil, height = nil, modifier = nil) ⇒ Geometry

Gives a Geometry representing the given height and width



8
9
10
11
12
13
14
# File 'lib/paperclip/geometry.rb', line 8

def initialize width = nil, height = nil, modifier = nil
  height = nil if height == ""
  width  = nil if width  == ""
  @height = (height || width).to_f
  @width  = (width  || height).to_f
  @modifier = modifier
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/paperclip/geometry.rb', line 5

def height
  @height
end

#modifierObject

Returns the value of attribute modifier.



5
6
7
# File 'lib/paperclip/geometry.rb', line 5

def modifier
  @modifier
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/paperclip/geometry.rb', line 5

def width
  @width
end

Class Method Details

.from_file(file) ⇒ Object

Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path.



18
19
20
21
22
# File 'lib/paperclip/geometry.rb', line 18

def self.from_file file
  file = file.path if file.respond_to? "path"
  parse(`#{Paperclip.path_for_command('identify')} "#{file}"`) ||
    raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
end

.parse(string) ⇒ Object

Parses a “WxH” formatted string, where W is the width and H is the height.



25
26
27
28
29
# File 'lib/paperclip/geometry.rb', line 25

def self.parse string
  if match = (string && string.match(/\b(\d*)x(\d*)\b([\>\<\#\@\%^!])?/))
    Geometry.new(*match[1,3])
  end
end

Instance Method Details

#aspectObject

The aspect ratio of the dimensions.



47
48
49
# File 'lib/paperclip/geometry.rb', line 47

def aspect
  width / height
end

#horizontal?Boolean

True if the dimensions represent a horizontal rectangle

Returns:

  • (Boolean)


37
38
39
# File 'lib/paperclip/geometry.rb', line 37

def horizontal?
  height < width
end

#inspectObject

Same as to_s



67
68
69
# File 'lib/paperclip/geometry.rb', line 67

def inspect
  to_s
end

#largerObject

Returns the larger of the two dimensions



52
53
54
# File 'lib/paperclip/geometry.rb', line 52

def larger
  [height, width].max
end

#smallerObject

Returns the smaller of the two dimensions



57
58
59
# File 'lib/paperclip/geometry.rb', line 57

def smaller
  [height, width].min
end

#square?Boolean

True if the dimensions represent a square

Returns:

  • (Boolean)


32
33
34
# File 'lib/paperclip/geometry.rb', line 32

def square?
  height == width
end

#to_sObject

Returns the width and height in a format suitable to be passed to Geometry.parse



62
63
64
# File 'lib/paperclip/geometry.rb', line 62

def to_s
  "%dx%d%s" % [width, height, modifier]
end

#transformation_to(dst, crop = false) ⇒ Object

Returns the scaling and cropping geometries (in string-based ImageMagick format) neccessary to transform this Geometry into the Geometry given. If crop is true, then it is assumed the destination Geometry will be the exact final resolution. In this case, the source Geometry is scaled so that an image containing the destination Geometry would be completely filled by the source image, and any overhanging image would be cropped. Useful for square thumbnail images. The cropping is weighted at the center of the Geometry.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/paperclip/geometry.rb', line 78

def transformation_to dst, crop = false

  if crop
    ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
    scale_geometry, scale = scaling(dst, ratio)
    crop_geometry         = cropping(dst, ratio, scale)
  else
    scale_geometry        = dst.to_s
  end
  
  [ scale_geometry, crop_geometry ]
end

#vertical?Boolean

True if the dimensions represent a vertical rectangle

Returns:

  • (Boolean)


42
43
44
# File 'lib/paperclip/geometry.rb', line 42

def vertical?
  height > width
end