Defines the geometry of an image.
- aspect
- from_file
- horizontal?
- inspect
- larger
- new
- parse
- smaller
- square?
- to_s
- transformation_to
- vertical?
| [RW] | height | |
| [RW] | modifier | |
| [RW] | width |
Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path.
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 17
17: def self.from_file(file)
18: file = file.path if file.respond_to? "path"
19: geometry = begin
20: Attachment.run("identify", %Q[-format "%wx%h" "#{file}"[0]])
21: rescue AttachmentCommandLineError
22: ""
23: end
24: parse(geometry) ||
25: raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
26: end
Gives a Geometry representing the given height and width
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 9
9: def initialize(width = nil, height = nil, modifier = nil)
10: @height = height.to_f
11: @width = width.to_f
12: @modifier = modifier
13: end
Parses a "WxH" formatted string, where W is the width and H is the height.
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 29
29: def self.parse(string)
30: if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/))
31: Geometry.new(*match[1,3])
32: end
33: end
The aspect ratio of the dimensions.
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 51
51: def aspect
52: width / height
53: end
True if the dimensions represent a horizontal rectangle
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 41
41: def horizontal?
42: height < width
43: end
Same as to_s
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 75
75: def inspect
76: to_s
77: end
Returns the larger of the two dimensions
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 56
56: def larger
57: [height, width].max
58: end
Returns the smaller of the two dimensions
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 61
61: def smaller
62: [height, width].min
63: end
True if the dimensions represent a square
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 36
36: def square?
37: height == width
38: end
Returns the width and height in a format suitable to be passed to Geometry.parse
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 66
66: def to_s
67: s = ""
68: s << width.to_i.to_s if width > 0
69: s << "x#{height.to_i}" if height > 0
70: s << modifier.to_s
71: s
72: end
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.
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 86
86: def transformation_to(dst, crop = false)
87: if crop
88: ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
89: scale_geometry, scale = scaling(dst, ratio)
90: crop_geometry = cropping(dst, ratio, scale)
91: else
92: scale_geometry = dst.to_s
93: end
94:
95: [ scale_geometry, crop_geometry ]
96: end
True if the dimensions represent a vertical rectangle
[ show source ]
# File lib/data_base/attachment/geometry.rb, line 46
46: def vertical?
47: height > width
48: end