class Paperclip::Geometry

Defines the geometry of an image.

Constants

EXIF_ROTATED_ORIENTATION_VALUES

Attributes

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

Public Class Methods

from_file(file) click to toggle source

Extracts the Geometry from a file (or path to a file)

# File lib/paperclip/geometry.rb, line 25
def self.from_file(file)
  GeometryDetector.new(file).make
end
new(width = nil, height = nil, modifier = nil) click to toggle source

Gives a Geometry representing the given height and width

# File lib/paperclip/geometry.rb, line 10
def initialize(width = nil, height = nil, modifier = nil)
  if width.is_a?(Hash)
    options = width
    @height = options[:height].to_f
    @width = options[:width].to_f
    @modifier = options[:modifier]
    @orientation = options[:orientation].to_i
  else
    @height = height.to_f
    @width  = width.to_f
    @modifier = modifier
  end
end
parse(string) click to toggle source

Extracts the Geometry from a “WxH,O” string Where W is the width, H is the height, and O is the EXIF orientation

# File lib/paperclip/geometry.rb, line 32
def self.parse(string)
  GeometryParser.new(string).make
end

Public Instance Methods

aspect() click to toggle source

The aspect ratio of the dimensions.

# File lib/paperclip/geometry.rb, line 60
def aspect
  width / height
end
auto_orient() click to toggle source

Swaps the height and width if necessary

# File lib/paperclip/geometry.rb, line 37
def auto_orient
  if EXIF_ROTATED_ORIENTATION_VALUES.include?(@orientation)
    @height, @width = @width, @height
    @orientation -= 4
  end
end
horizontal?() click to toggle source

True if the dimensions represent a horizontal rectangle

# File lib/paperclip/geometry.rb, line 50
def horizontal?
  height < width
end
inspect() click to toggle source

Same as to_s

# File lib/paperclip/geometry.rb, line 84
def inspect
  to_s
end
larger() click to toggle source

Returns the larger of the two dimensions

# File lib/paperclip/geometry.rb, line 65
def larger
  [height, width].max
end
resize_to(geometry) click to toggle source

resize to a new geometry @param geometry [String] the Paperclip geometry definition to resize to @example

Paperclip::Geometry.new(150, 150).resize_to('50x50!')
#=> Paperclip::Geometry(50, 50)
# File lib/paperclip/geometry.rb, line 112
def resize_to(geometry)
  new_geometry = Paperclip::Geometry.parse geometry
  case new_geometry.modifier
  when '!', '#'
    new_geometry
  when '>'
    if new_geometry.width >= self.width && new_geometry.height >= self.height
      self
    else
      scale_to new_geometry
    end
  when '<'
    if new_geometry.width <= self.width || new_geometry.height <= self.height
      self
    else
      scale_to new_geometry
    end
  else
    scale_to new_geometry
  end
end
smaller() click to toggle source

Returns the smaller of the two dimensions

# File lib/paperclip/geometry.rb, line 70
def smaller
  [height, width].min
end
square?() click to toggle source

True if the dimensions represent a square

# File lib/paperclip/geometry.rb, line 45
def square?
  height == width
end
to_s() click to toggle source

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

# File lib/paperclip/geometry.rb, line 75
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
transformation_to(dst, crop = false) click to toggle source

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/paperclip/geometry.rb, line 95
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
vertical?() click to toggle source

True if the dimensions represent a vertical rectangle

# File lib/paperclip/geometry.rb, line 55
def vertical?
  height > width
end

Private Instance Methods

cropping(dst, ratio, scale) click to toggle source
# File lib/paperclip/geometry.rb, line 144
def cropping dst, ratio, scale
  if ratio.horizontal? || ratio.square?
    "%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
  else
    "%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ]
  end
end
scale_to(new_geometry) click to toggle source

scale to the requested geometry and preserve the aspect ratio

# File lib/paperclip/geometry.rb, line 153
def scale_to(new_geometry)
  scale = [new_geometry.width.to_f / self.width.to_f , new_geometry.height.to_f / self.height.to_f].min
  Paperclip::Geometry.new((self.width * scale).round, (self.height * scale).round)
end
scaling(dst, ratio) click to toggle source
# File lib/paperclip/geometry.rb, line 136
def scaling dst, ratio
  if ratio.horizontal? || ratio.square?
    [ "%dx" % dst.width, ratio.width ]
  else
    [ "x%d" % dst.height, ratio.height ]
  end
end