| 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 18
18: def self.from_file file
19: file = file.path if file.respond_to? "path"
20: parse(`#{Paperclip.path_for_command('identify')} "#{file}"`) ||
21: raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
22: end
Gives a Geometry representing the given height and width
# File lib/paperclip/geometry.rb, line 8
8: def initialize width = nil, height = nil, modifier = nil
9: height = nil if height == ""
10: width = nil if width == ""
11: @height = (height || width).to_f
12: @width = (width || height).to_f
13: @modifier = modifier
14: end
Parses a "WxH" formatted string, where W is the width and H is the height.
# File lib/paperclip/geometry.rb, line 25
25: def self.parse string
26: if match = (string && string.match(/\b(\d*)x(\d*)\b([\>\<\#\@\%^!])?/))
27: Geometry.new(*match[1,3])
28: end
29: end
True if the dimensions represent a horizontal rectangle
# File lib/paperclip/geometry.rb, line 37
37: def horizontal?
38: height < width
39: end
True if the dimensions represent a square
# File lib/paperclip/geometry.rb, line 32
32: def square?
33: height == width
34: end
Returns the width and height in a format suitable to be passed to Geometry.parse
# File lib/paperclip/geometry.rb, line 62
62: def to_s
63: "%dx%d%s" % [width, height, modifier]
64: 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 78
78: def transformation_to dst, crop = false
79: ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
80:
81: if crop
82: scale_geometry, scale = scaling(dst, ratio)
83: crop_geometry = cropping(dst, ratio, scale)
84: else
85: scale_geometry = dst.to_s
86: end
87:
88: [ scale_geometry, crop_geometry ]
89: end