Class Paperclip::Geometry
In: lib/paperclip/geometry.rb
Parent: Object

Defines the geometry of an image.

Methods

aspect   from_file   horizontal?   inspect   larger   new   parse   smaller   square?   to_s   transformation_to   vertical?  

Attributes

height  [RW] 
modifier  [RW] 
width  [RW] 

Public Class methods

Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path.

[Source]

    # 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

[Source]

    # 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.

[Source]

    # 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

Public Instance methods

The aspect ratio of the dimensions.

[Source]

    # File lib/paperclip/geometry.rb, line 47
47:     def aspect
48:       width / height
49:     end

True if the dimensions represent a horizontal rectangle

[Source]

    # File lib/paperclip/geometry.rb, line 37
37:     def horizontal?
38:       height < width
39:     end

Same as to_s

[Source]

    # File lib/paperclip/geometry.rb, line 67
67:     def inspect
68:       to_s
69:     end

Returns the larger of the two dimensions

[Source]

    # File lib/paperclip/geometry.rb, line 52
52:     def larger
53:       [height, width].max
54:     end

Returns the smaller of the two dimensions

[Source]

    # File lib/paperclip/geometry.rb, line 57
57:     def smaller
58:       [height, width].min
59:     end

True if the dimensions represent a square

[Source]

    # 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

[Source]

    # 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.

[Source]

    # 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

True if the dimensions represent a vertical rectangle

[Source]

    # File lib/paperclip/geometry.rb, line 42
42:     def vertical?
43:       height > width
44:     end

[Validate]