Class: Paperclip::Geometry

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

Overview

Defines the geometry of an image.

Constant Summary collapse

EXIF_ROTATED_ORIENTATION_VALUES =
[5, 6, 7, 8].freeze

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


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paperclip/geometry.rb', line 9

def initialize(width = nil, height = nil, modifier = nil)
  if width.is_a?(Hash)
    options = width
    @height = options[:height].to_f
    @width = options[:width].to_f
    @modifier = options[:modifier]
    @orientation = options[:orientation].to_i
  else
    @height = height.to_f
    @width  = width.to_f
    @modifier = modifier
  end
end

Instance Attribute Details

#heightObject

Returns the value of attribute height


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

def height
  @height
end

#modifierObject

Returns the value of attribute modifier


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

def modifier
  @modifier
end

#widthObject

Returns the value of attribute width


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

def width
  @width
end

Class Method Details

.from_file(file) ⇒ Object

Extracts the Geometry from a file (or path to a file)


24
25
26
# File 'lib/paperclip/geometry.rb', line 24

def self.from_file(file)
  GeometryDetector.new(file).make
end

.parse(string) ⇒ Object

Extracts the Geometry from a “WxH,O” string Where W is the width, H is the height, and O is the EXIF orientation


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

def self.parse(string)
  GeometryParser.new(string).make
end

Instance Method Details

#aspectObject

The aspect ratio of the dimensions.


59
60
61
# File 'lib/paperclip/geometry.rb', line 59

def aspect
  width / height
end

#auto_orientObject

Swaps the height and width if necessary


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

def auto_orient
  if EXIF_ROTATED_ORIENTATION_VALUES.include?(@orientation)
    @height, @width = @width, @height
    @orientation -= 4
  end
end

#horizontal?Boolean

True if the dimensions represent a horizontal rectangle

Returns:

  • (Boolean)

49
50
51
# File 'lib/paperclip/geometry.rb', line 49

def horizontal?
  height < width
end

#inspectObject

Same as to_s


83
84
85
# File 'lib/paperclip/geometry.rb', line 83

def inspect
  to_s
end

#largerObject

Returns the larger of the two dimensions


64
65
66
# File 'lib/paperclip/geometry.rb', line 64

def larger
  [height, width].max
end

#resize_to(geometry) ⇒ Object

resize to a new geometry

Examples:

Paperclip::Geometry.new(150, 150).resize_to('50x50!')
#=> Paperclip::Geometry(50, 50)

Parameters:

  • geometry (String)

    the Paperclip geometry definition to resize to


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/paperclip/geometry.rb', line 111

def resize_to(geometry)
  new_geometry = Paperclip::Geometry.parse geometry
  case new_geometry.modifier
  when "!", "#"
    new_geometry
  when ">"
    if new_geometry.width >= width && new_geometry.height >= height
      self
    else
      scale_to new_geometry
    end
  when "<"
    if new_geometry.width <= width || new_geometry.height <= height
      self
    else
      scale_to new_geometry
    end
  else
    scale_to new_geometry
  end
end

#smallerObject

Returns the smaller of the two dimensions


69
70
71
# File 'lib/paperclip/geometry.rb', line 69

def smaller
  [height, width].min
end

#square?Boolean

True if the dimensions represent a square

Returns:

  • (Boolean)

44
45
46
# File 'lib/paperclip/geometry.rb', line 44

def square?
  height == width
end

#to_sObject

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


74
75
76
77
78
79
80
# File 'lib/paperclip/geometry.rb', line 74

def to_s
  s = ""
  s << width.to_i.to_s if width > 0
  s << "x#{height.to_i}" if height > 0
  s << modifier.to_s
  s
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.


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/paperclip/geometry.rb', line 94

def transformation_to(dst, crop = false)
  if crop
    ratio = Geometry.new(dst.width / width, dst.height / 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)

54
55
56
# File 'lib/paperclip/geometry.rb', line 54

def vertical?
  height > width
end