Class: Paperclip::Geometry
- Inherits:
-
Object
- Object
- Paperclip::Geometry
- 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
-
#height ⇒ Object
Returns the value of attribute height.
-
#modifier ⇒ Object
Returns the value of attribute modifier.
-
#orientation ⇒ Object
Returns the value of attribute orientation.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
-
.from_file(file) ⇒ Object
deprecated
Deprecated.
Will be removed in Paperclip 8.0.
-
.parse(string) ⇒ Object
deprecated
Deprecated.
Will be removed in Paperclip 8.0.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#aspect ⇒ Object
The aspect ratio of the dimensions.
-
#auto_orient ⇒ Object
Swaps the height and width if necessary.
-
#horizontal? ⇒ Boolean
True if the dimensions represent a horizontal rectangle.
-
#initialize(width = nil, height = nil, modifier = nil, orientation = nil) ⇒ Geometry
constructor
Gives a Geometry representing the given height and width.
-
#inspect ⇒ Object
Same as to_s.
-
#larger ⇒ Object
Returns the larger of the two dimensions.
-
#resize_to(geometry) ⇒ Object
resize to a new geometry.
-
#smaller ⇒ Object
Returns the smaller of the two dimensions.
-
#square? ⇒ Boolean
True if the dimensions represent a square.
-
#to_s ⇒ Object
Returns the width and height in a format suitable to be passed to Geometry.parse.
-
#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.
-
#vertical? ⇒ Boolean
True if the dimensions represent a vertical rectangle.
Constructor Details
#initialize(width = nil, height = nil, modifier = nil, orientation = nil) ⇒ Geometry
Gives a Geometry representing the given height and width
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/paperclip/geometry.rb', line 11 def initialize(width = nil, height = nil, modifier = nil, orientation = nil) if width.is_a?(Hash) = width @height = [:height].to_f @width = [:width].to_f @modifier = [:modifier].presence @orientation = [:orientation].to_i else @height = height.to_f @width = width.to_f @modifier = modifier.presence @orientation = orientation.to_i end end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
6 7 8 |
# File 'lib/paperclip/geometry.rb', line 6 def height @height end |
#modifier ⇒ Object
Returns the value of attribute modifier.
6 7 8 |
# File 'lib/paperclip/geometry.rb', line 6 def modifier @modifier end |
#orientation ⇒ Object
Returns the value of attribute orientation.
6 7 8 |
# File 'lib/paperclip/geometry.rb', line 6 def orientation @orientation end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/paperclip/geometry.rb', line 6 def width @width end |
Class Method Details
.from_file(file) ⇒ Object
Will be removed in Paperclip 8.0.
Extracts the Geometry from a file (or path to a file)
29 30 31 |
# File 'lib/paperclip/geometry.rb', line 29 def self.from_file(file) Commands::ImageMagick::GeometryParser.from_file(file) end |
.parse(string) ⇒ Object
Will be removed in Paperclip 8.0.
Extracts the Geometry from a "WxH,O" string Where W is the width, H is the height, and O is the EXIF orientation
38 39 40 |
# File 'lib/paperclip/geometry.rb', line 38 def self.parse(string) Commands::ImageMagick::GeometryParser.parse(string) end |
Instance Method Details
#==(other) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/paperclip/geometry.rb', line 89 def ==(other) other.is_a?(Geometry) && width == other.width && height == other.height && modifier == other.modifier && orientation == other.orientation end |
#aspect ⇒ Object
The aspect ratio of the dimensions.
66 67 68 |
# File 'lib/paperclip/geometry.rb', line 66 def aspect width / height end |
#auto_orient ⇒ Object
Swaps the height and width if necessary
43 44 45 46 47 48 |
# File 'lib/paperclip/geometry.rb', line 43 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
56 57 58 |
# File 'lib/paperclip/geometry.rb', line 56 def horizontal? height < width end |
#inspect ⇒ Object
Same as to_s
98 99 100 |
# File 'lib/paperclip/geometry.rb', line 98 def inspect to_s end |
#larger ⇒ Object
Returns the larger of the two dimensions
71 72 73 |
# File 'lib/paperclip/geometry.rb', line 71 def larger [height, width].max end |
#resize_to(geometry) ⇒ Object
resize to a new geometry
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/paperclip/geometry.rb', line 126 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 |
#smaller ⇒ Object
Returns the smaller of the two dimensions
76 77 78 |
# File 'lib/paperclip/geometry.rb', line 76 def smaller [height, width].min end |
#square? ⇒ Boolean
True if the dimensions represent a square
51 52 53 |
# File 'lib/paperclip/geometry.rb', line 51 def square? height == width end |
#to_s ⇒ Object
Returns the width and height in a format suitable to be passed to Geometry.parse
81 82 83 84 85 86 87 |
# File 'lib/paperclip/geometry.rb', line 81 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.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/paperclip/geometry.rb', line 109 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
61 62 63 |
# File 'lib/paperclip/geometry.rb', line 61 def vertical? height > width end |