Defines the geometry of an image.
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 def self.from_file file file = file.path if file.respond_to? "path" geometry = begin Paperclip.run("identify", %[-format "%wx%h" "#{file}"[0]]) rescue PaperclipCommandLineError "" end parse(geometry) || raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) end
Gives a Geometry representing the given height and width
# File lib/dm-paperclip/geometry.rb, line 8 def initialize width = nil, height = nil, modifier = nil height = nil if height == '' width = nil if width == '' @height = (height || width).to_f @width = (width || height).to_f @modifier = modifier end
The aspect ratio of the dimensions.
# File lib/dm-paperclip/geometry.rb, line 52 def aspect width / height end
True if the dimensions represent a horizontal rectangle
# File lib/dm-paperclip/geometry.rb, line 42 def horizontal? height < width end
Same as to_s
# File lib/dm-paperclip/geometry.rb, line 76 def inspect to_s end
Returns the larger of the two dimensions
# File lib/dm-paperclip/geometry.rb, line 57 def larger [height, width].max end
Returns the smaller of the two dimensions
# File lib/dm-paperclip/geometry.rb, line 62 def smaller [height, width].min end
True if the dimensions represent a square
# File lib/dm-paperclip/geometry.rb, line 37 def square? height == width end
Returns the width and height in a format suitable to be passed to Geometry.parse
# File lib/dm-paperclip/geometry.rb, line 67 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
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 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
Generated with the Darkfish Rdoc Generator 2.