Class: Refinery::ThumbnailDimensions

Inherits:
Object
  • Object
show all
Defined in:
images/app/models/refinery/thumbnail_dimensions.rb

Defined Under Namespace

Classes: Geometry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(geometry, original_width, original_height) ⇒ ThumbnailDimensions



6
7
8
9
10
11
12
13
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 6

def initialize(geometry, original_width, original_height)
  @geometry = Geometry.new(geometry)

  @original_width = original_width.to_f
  @original_height = original_height.to_f

  process
end

Instance Attribute Details

#geometryObject

Returns the value of attribute geometry



4
5
6
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 4

def geometry
  @geometry
end

#heightObject

Returns the value of attribute height



4
5
6
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 4

def height
  @height
end

#original_heightObject

Returns the value of attribute original_height



4
5
6
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 4

def original_height
  @original_height
end

#original_widthObject

Returns the value of attribute original_width



4
5
6
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 4

def original_width
  @original_width
end

#widthObject

Returns the value of attribute width



4
5
6
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 4

def width
  @width
end

Instance Method Details

#cropObject



45
46
47
48
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 45

def crop
  @width = geometry.width
  @height = geometry.height
end

#original_dimensionsObject



40
41
42
43
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 40

def original_dimensions
  @width = original_width
  @height = original_height
end

#processObject



15
16
17
18
19
20
21
22
23
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 15

def process
  if valid_geometry && resize_geometry && resize_necessary
    scale
  elsif valid_geometry && !resize_geometry
    crop
  else
    original_dimensions
  end
end

#resize_geometryObject



29
30
31
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 29

def resize_geometry
  ::Dragonfly::ImageMagick::Processors::Thumb::RESIZE_GEOMETRY === geometry.geometry
end

#resize_necessaryObject



33
34
35
36
37
38
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 33

def resize_necessary
  !geometry.custom? ||
      (geometry.custom? &&
        (original_width > geometry.width ||
          original_height > geometry.height))
end

#scaleObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 50

def scale
  # Try scaling with width factor first. (wf = width factor)
  wf_width = geometry.width.round
  wf_height = (original_height * geometry.width / original_width).round

  # Scale with height factor (hf = height factor)
  hf_width = (original_width * geometry.height / original_height).round
  hf_height = geometry.height.round

  # Take the highest value that doesn't exceed either axis limit.
  use_wf = wf_width > 0 && wf_width <= geometry.width && wf_height <= geometry.height
  if use_wf && hf_width <= geometry.width && hf_height <= geometry.height
    use_wf = wf_width * wf_height > hf_width * hf_height
  end

  if use_wf
    @width = wf_width
    @height = wf_height
  else
    @width = hf_width
    @height = hf_height
  end
end

#thumb_geometryObject



74
75
76
77
78
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 74

def thumb_geometry
  Regexp.union(::Dragonfly::ImageMagick::Processors::Thumb::RESIZE_GEOMETRY,
               ::Dragonfly::ImageMagick::Processors::Thumb::CROPPED_RESIZE_GEOMETRY,
               ::Dragonfly::ImageMagick::Processors::Thumb::CROP_GEOMETRY)
end

#valid_geometryObject



25
26
27
# File 'images/app/models/refinery/thumbnail_dimensions.rb', line 25

def valid_geometry
  (original_width * original_height > 0) && thumb_geometry === geometry.geometry
end