Class: Paperclip::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/geometry_transformation.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Tests whether two geometries are identical in dimensions and modifier.



43
44
45
# File 'lib/paperclip/geometry_transformation.rb', line 43

def == (other)
  self.to_s == other.to_s
end

#=~(other) ⇒ Object

Tests whether two geometries have the same dimensions, ignoring modifier.



48
49
50
# File 'lib/paperclip/geometry_transformation.rb', line 48

def =~ (other)
  self.height.to_i == other.height.to_i && self.width.to_i == other.width.to_i
end

#scaled_by(other) ⇒ Object

Scales this geometry by the percentage(s) specified in that geometry.



71
72
73
74
75
76
77
78
# File 'lib/paperclip/geometry_transformation.rb', line 71

def scaled_by(other)
  other = Geometry.new("#{other}%") unless other.is_a? Geometry
  if other.height > 0
    Geometry.new(self.width * other.width / 100, self.height * other.height / 100)
  else
    Geometry.new(self.width * other.width / 100, self.height * other.width / 100)
  end
end

#scaled_to_fit(other) ⇒ Object

Scales this geometry to fit within that geometry.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/paperclip/geometry_transformation.rb', line 53

def scaled_to_fit(other)
  if (other.width > 0 && other.height == 0)
    Geometry.new(other.width, self.height * other.width / self.width)
  elsif (other.width == 0 && other.height > 0)
    Geometry.new(self.width * other.height / self.height, other.height)
  else
    ratio = Geometry.new( other.width / self.width, other.height / self.height )
    if ratio.square?
      other.without_modifier
    elsif ratio.horizontal?
      Geometry.new(ratio.height * self.width, other.height)
    else
      Geometry.new(other.width, ratio.width * self.height)
    end
  end
end

#transformed_by(other) ⇒ Object Also known as: *

Returns the dimensions that would result if a thumbnail was created by transforming this geometry into that geometry. Its purpose is to mimic imagemagick conversions. Used like so:

file_geometry.transformed_by(style_geometry)

it returns the size of the thumbnail image you would get by applying that rule. This saves us having to go back to the file, which is expensive with S3. We understand all the Imagemagick geometry arguments described at www.imagemagick.org/script/command-line-processing.php#geometry including both ‘^’ and paperclip’s own ‘#’ modifier.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/paperclip/geometry_transformation.rb', line 21

def transformed_by (other)
  other = Geometry.parse(other) unless other.is_a? Geometry
  # if the two geometries are similar, or the destination geometry is a fixed size, the resulting dimensions are fixed
  return other.without_modifier if self =~ other || ['#', '!', '^'].include?(other.modifier)
  # otherwise, we apply the transformation
  raise TransformationError, "geometry is not transformable without both width and height" if self.height == 0 or self.width == 0
  case other.modifier
  when '>'
    (other.width < self.width || other.height < self.height) ? scaled_to_fit(other) : self
  when '<'
    (other.width > self.width && other.height > self.height) ? scaled_to_fit(other) : self
  when '%'
    scaled_by(other)
  when '@'
    scaled_by(other.width * 100 / (self.width * self.height))
  else
    scaled_to_fit(other)
  end
end

#without_modifierObject

Returns a new Geometry object with the the same dimensions as this but with no modifier.



9
10
11
# File 'lib/paperclip/geometry_transformation.rb', line 9

def without_modifier
  Geometry.new(self.width, self.height)
end