Class: Lipsiadmin::Attachment::Geometry
- Inherits:
-
Object
- Object
- Lipsiadmin::Attachment::Geometry
- Defined in:
- lib/data_base/attachment/geometry.rb
Overview
Defines the geometry of an image.
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#modifier ⇒ Object
Returns the value of attribute modifier.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
-
.from_file(file) ⇒ Object
Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path.
-
.parse(string) ⇒ Object
Parses a “WxH” formatted string, where W is the width and H is the height.
Instance Method Summary collapse
-
#aspect ⇒ Object
The aspect ratio of the dimensions.
-
#horizontal? ⇒ Boolean
True if the dimensions represent a horizontal rectangle.
-
#initialize(width = nil, height = nil, modifier = 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.
-
#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) ⇒ Geometry
Gives a Geometry representing the given height and width
9 10 11 12 13 |
# File 'lib/data_base/attachment/geometry.rb', line 9 def initialize(width = nil, height = nil, modifier = nil) @height = height.to_f @width = width.to_f @modifier = modifier end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
6 7 8 |
# File 'lib/data_base/attachment/geometry.rb', line 6 def height @height end |
#modifier ⇒ Object
Returns the value of attribute modifier.
6 7 8 |
# File 'lib/data_base/attachment/geometry.rb', line 6 def modifier @modifier end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/data_base/attachment/geometry.rb', line 6 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.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/data_base/attachment/geometry.rb', line 17 def self.from_file(file) file = file.path if file.respond_to? "path" geometry = begin Attachment.run("identify", %Q[-format "%wx%h" "#{file}"[0]]) rescue AttachmentCommandLineError "" end parse(geometry) || raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) end |
Instance Method Details
#aspect ⇒ Object
The aspect ratio of the dimensions.
51 52 53 |
# File 'lib/data_base/attachment/geometry.rb', line 51 def aspect width / height end |
#horizontal? ⇒ Boolean
True if the dimensions represent a horizontal rectangle
41 42 43 |
# File 'lib/data_base/attachment/geometry.rb', line 41 def horizontal? height < width end |
#inspect ⇒ Object
Same as to_s
75 76 77 |
# File 'lib/data_base/attachment/geometry.rb', line 75 def inspect to_s end |
#larger ⇒ Object
Returns the larger of the two dimensions
56 57 58 |
# File 'lib/data_base/attachment/geometry.rb', line 56 def larger [height, width].max end |
#smaller ⇒ Object
Returns the smaller of the two dimensions
61 62 63 |
# File 'lib/data_base/attachment/geometry.rb', line 61 def smaller [height, width].min end |
#square? ⇒ Boolean
True if the dimensions represent a square
36 37 38 |
# File 'lib/data_base/attachment/geometry.rb', line 36 def square? height == width end |
#to_s ⇒ Object
Returns the width and height in a format suitable to be passed to Geometry.parse
66 67 68 69 70 71 72 |
# File 'lib/data_base/attachment/geometry.rb', line 66 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.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/data_base/attachment/geometry.rb', line 86 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
46 47 48 |
# File 'lib/data_base/attachment/geometry.rb', line 46 def vertical? height > width end |