Module: ImageSizer

Defined in:
lib/image_sizer.rb

Class Method Summary collapse

Class Method Details

.crop(width, height, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/image_sizer.rb', line 20

def self.crop(width, height, opts = {})
  return if width.blank? || height.blank?

  max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f
  max_height = (opts[:max_height] || SiteSetting.max_image_height).to_f

  w = width.to_f
  h = height.to_f

  return w.floor, h.floor if w <= max_width && h <= max_height

  ratio = max_width / w

  [[max_width, w].min.floor, [max_height, (h * ratio)].min.floor]
end

.resize(width, height, opts = {}) ⇒ Object

Resize an image to the aspect ratio we want



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/image_sizer.rb', line 5

def self.resize(width, height, opts = {})
  return if width.blank? || height.blank?

  max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f
  max_height = (opts[:max_height] || SiteSetting.max_image_height).to_f

  w = width.to_f
  h = height.to_f

  return w.floor, h.floor if w <= max_width && h <= max_height

  ratio = [max_width / w, max_height / h].min
  [(w * ratio).floor, (h * ratio).floor]
end