class Paperclip::Geometry

Defines the geometry of an image.

Attributes

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

Public Class Methods

from_file(file) click to toggle source

Uses ImageMagick to determing the dimensions of a file, passed in as either a File or path. NOTE: (race cond) Do not reassign the ‘file’ variable inside this method as it is likely to be a Tempfile object, which would be eligible for file deletion when no longer referenced.

# File lib/paperclip/geometry.rb, line 18
def self.from_file file
  file_path = file.respond_to?(:path) ? file.path : file
  raise(Errors::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
  geometry = begin
               silence_stream(STDERR) do
                 Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
               end
             rescue Cocaine::ExitStatusError
               ""
             rescue Cocaine::CommandNotFoundError => e
               raise Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
             end
  parse(geometry) ||
    raise(Errors::NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
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 8
def initialize width = nil, height = nil, modifier = nil
  @height = height.to_f
  @width  = width.to_f
  @modifier = modifier
end
parse(string) click to toggle source

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

# File lib/paperclip/geometry.rb, line 35
def self.parse string
  if match = (string && string.match(%r\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/))
    Geometry.new(*match[1,3])
  end
end

Public Instance Methods

aspect() click to toggle source

The aspect ratio of the dimensions.

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

True if the dimensions represent a horizontal rectangle

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

Same as #to_s

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

Returns the larger of the two dimensions

# File lib/paperclip/geometry.rb, line 62
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 109
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 67
def smaller
  [height, width].min
end
square?() click to toggle source

True if the dimensions represent a square

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

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

# File lib/paperclip/geometry.rb, line 72
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 92
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 52
def vertical?
  height > width
end