class Paperclip::Geometry
Defines the geometry of an image.
def self.from_file(file)
def self.from_file(file) GeometryDetector.new(file).make end
def self.parse(string)
Where W is the width, H is the height,
Extracts the Geometry from a "WxH,O" string
def self.parse(string) GeometryParser.new(string).make end
def aspect
def aspect width / height end
def auto_orient
def auto_orient if EXIF_ROTATED_ORIENTATION_VALUES.include?(@orientation) @height, @width = @width, @height @orientation -= 4 end end
def cropping dst, ratio, scale
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
def horizontal?
def horizontal? height < width end
def initialize(width = nil, height = nil, modifier = nil)
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
def inspect
def inspect to_s end
def larger
def larger [height, width].max end
def resize_to(geometry)
-
geometry
(String
) -- the Paperclip geometry definition to resize to
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
def scale_to(new_geometry)
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
def scaling dst, ratio
def scaling dst, ratio if ratio.horizontal? || ratio.square? [ "%dx" % dst.width, ratio.width ] else [ "x%d" % dst.height, ratio.height ] end end
def smaller
def smaller [height, width].min end
def square?
def square? height == width end
def to_s
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
def transformation_to dst, crop = false
overhanging image would be cropped. Useful for square thumbnail images. The cropping
destination Geometry would be completely filled by the source image, and any
In this case, the source Geometry is scaled so that an image containing the
then it is assumed the destination Geometry will be the exact final resolution.
neccessary to transform this Geometry into the Geometry given. If crop is true,
Returns the scaling and cropping geometries (in string-based ImageMagick format)
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
def vertical?
def vertical? height > width end