Class | Paperclip::Geometry |
In: |
lib/dm-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/dm-paperclip/geometry.rb, line 18 18: def self.from_file file 19: file = file.path if file.respond_to? "path" 20: geometry = begin 21: Paperclip.run("identify", %Q[-format "%wx%h" "#{file}"[0]]) 22: rescue PaperclipCommandLineError 23: "" 24: end 25: parse(geometry) || 26: raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) 27: end
Gives a Geometry representing the given height and width
# File lib/dm-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/dm-paperclip/geometry.rb, line 30 30: def self.parse string 31: if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/i)) 32: Geometry.new(*match[1,3]) 33: end 34: end
True if the dimensions represent a horizontal rectangle
# File lib/dm-paperclip/geometry.rb, line 42 42: def horizontal? 43: height < width 44: end
True if the dimensions represent a square
# File lib/dm-paperclip/geometry.rb, line 37 37: def square? 38: height == width 39: end
Returns the width and height in a format suitable to be passed to Geometry.parse
# File lib/dm-paperclip/geometry.rb, line 67 67: def to_s 68: s = "" 69: s << width.to_i.to_s if width > 0 70: s << "x#{height.to_i}" if height > 0 71: s << modifier.to_s 72: s 73: 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/dm-paperclip/geometry.rb, line 87 87: def transformation_to dst, crop = false 88: if crop 89: ratio = Geometry.new( dst.width / self.width, dst.height / self.height ) 90: scale_geometry, scale = scaling(dst, ratio) 91: crop_geometry = cropping(dst, ratio, scale) 92: else 93: scale_geometry = dst.to_s 94: end 95: 96: [ scale_geometry, crop_geometry ] 97: end