Class | Paperclip::Geometry
In: | vendor/plugins/paperclip/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

from_file(file)

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

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 16
    16:     def self.from_file file
    17:       file = file.path if file.respond_to? "path"
    18:       geometry = begin
    19:                    Paperclip.run("identify", %Q[-format "%wx%h" "#{file}"[0]])
    20:                  rescue PaperclipCommandLineError
    21:                    ""
    22:                  end
    23:       parse(geometry) ||
    24:         raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
    25:     end

new(width = nil, height = nil, modifier = nil)

Gives a Geometry representing the given height and width

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 8
     8:     def initialize width = nil, height = nil, modifier = nil
     9:       @height = height.to_f
    10:       @width  = width.to_f
    11:       @modifier = modifier
    12:     end

parse(string)

Parses a "WxH" formatted string, where W is the width and H is the height.

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 28
    28:     def self.parse string
    29:       if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/))
    30:         Geometry.new(*match[1,3])
    31:       end
    32:     end

### Public Instance methods

aspect()

The aspect ratio of the dimensions.

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 50
    50:     def aspect
    51:       width / height
    52:     end

horizontal?()

True if the dimensions represent a horizontal rectangle

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 40
    40:     def horizontal?
    41:       height < width
    42:     end

inspect()

Same as to_s

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 74
    74:     def inspect
    75:       to_s
    76:     end

larger()

Returns the larger of the two dimensions

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 55
    55:     def larger
    56:       [height, width].max
    57:     end

smaller()

Returns the smaller of the two dimensions

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 60
    60:     def smaller
    61:       [height, width].min
    62:     end

square?()

True if the dimensions represent a square

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 35
    35:     def square?
    36:       height == width
    37:     end

to_s()

Returns the width and height in a format suitable to be passed to Geometry.parse

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 65
    65:     def to_s
    66:       s = ""
    67:       s << width.to_i.to_s if width > 0
    68:       s << "x#{height.to_i}" if height > 0
    69:       s << modifier.to_s
    70:       s
    71:     end

transformation_to(dst, crop = false)

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 vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 85
    85:     def transformation_to dst, crop = false
    86:       if crop
    87:         ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
    88:         scale_geometry, scale = scaling(dst, ratio)
    89:         crop_geometry         = cropping(dst, ratio, scale)
    90:       else
    91:         scale_geometry        = dst.to_s
    92:       end
    93:
    94:       [ scale_geometry, crop_geometry ]
    95:     end

vertical?()

True if the dimensions represent a vertical rectangle

[Source]

        # File vendor/plugins/paperclip/lib/paperclip/geometry.rb, line 45
    45:     def vertical?
    46:       height > width
    47:     end

[Validate]