| Class | Paperclip::Geometry |
| In: |
lib/paperclip/geometry.rb
|
| Parent: | Object |
Defines the geometry of an image.
| height | [RW] | |
| modifier | [RW] | |
| width | [RW] |
Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path.
# File lib/paperclip/geometry.rb, line 16
16: def self.from_file file
17: file = file.path if file.respond_to? "path"
18: geometry = begin
19: Paperclip.run("identify", "-format %wx%h :file", :file => "#{file}[0]")
20: rescue PaperclipCommandLineError
21: ""
22: end
23: parse(geometry) ||
24: raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
25: end
Parses a "WxH" formatted string, where W is the width and H is the height.
# File lib/paperclip/geometry.rb, line 28
28: def self.parse string
29: if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/i))
30: Geometry.new(*match[1,3])
31: end
32: end
True if the dimensions represent a horizontal rectangle
# File lib/paperclip/geometry.rb, line 40
40: def horizontal?
41: height < width
42: end
True if the dimensions represent a square
# File lib/paperclip/geometry.rb, line 35
35: def square?
36: height == width
37: end
Returns the width and height in a format suitable to be passed to Geometry.parse
# File lib/paperclip/geometry.rb, line 65
65: def to_s
66: s = ""
67: s << width.to_i.to_s if width > 0
68: s << "x#{height.to_i}" if height > 0
69: s << modifier.to_s
70: s
71: 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.
# File lib/paperclip/geometry.rb, line 85
85: def transformation_to dst, crop = false
86: if crop
87: ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
88: scale_geometry, scale = scaling(dst, ratio)
89: crop_geometry = cropping(dst, ratio, scale)
90: else
91: scale_geometry = dst.to_s
92: end
93:
94: [ scale_geometry, crop_geometry ]
95: end