Class Paperclip::Geometry
In: lib/dm-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/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

[Source]

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

[Source]

    # 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

Public Instance methods

The aspect ratio of the dimensions.

[Source]

    # File lib/dm-paperclip/geometry.rb, line 52
52:     def aspect
53:       width / height
54:     end

True if the dimensions represent a horizontal rectangle

[Source]

    # File lib/dm-paperclip/geometry.rb, line 42
42:     def horizontal?
43:       height < width
44:     end

Same as to_s

[Source]

    # File lib/dm-paperclip/geometry.rb, line 76
76:     def inspect
77:       to_s
78:     end

Returns the larger of the two dimensions

[Source]

    # File lib/dm-paperclip/geometry.rb, line 57
57:     def larger
58:       [height, width].max
59:     end

Returns the smaller of the two dimensions

[Source]

    # File lib/dm-paperclip/geometry.rb, line 62
62:     def smaller
63:       [height, width].min
64:     end

True if the dimensions represent a square

[Source]

    # 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

[Source]

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

[Source]

    # 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

True if the dimensions represent a vertical rectangle

[Source]

    # File lib/dm-paperclip/geometry.rb, line 47
47:     def vertical?
48:       height > width
49:     end

[Validate]