Module: FacebookCoverResize

Defined in:
lib/facebook_cover_resize.rb,
lib/facebook_cover_resize/railtie.rb,
lib/facebook_cover_resize/version.rb,
lib/facebook_cover_resize/view_helpers.rb

Defined Under Namespace

Modules: ViewHelpers Classes: Railtie

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.compute(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facebook_cover_resize.rb', line 5

def self.compute(args)
  ow = args[:width].to_f
  oh = (ow / args[:ratio])

  if args[:original].any? && args[:offsets].any?
    nw = args[:original].first.to_f
    nh = args[:original].last.to_f
    ox = args[:offsets].first.to_f
    oy = args[:offsets].last.to_f

    if ox == 0 && oy == 0
      w = ow
      h = nh * (ow / nw)
      if h < oh
        h = oh
        w = nw * (oh / nh)
        offset_x = ow - w
        offset_y = 0
      else
        offset_x = 0
        if w < h
          offset_y = 0
        else
          offset_y = oh - h
        end
      end
    else
      if oy > 0
        w = ow
        h = nh * (ow / nw)
        offset_x = 0
        offset_y = (oh - h) * (oy / 100.0)
        if h < oh
          h = oh
          w = nw * (oh / nh)
          offset_x = (ow - w) / 2
          offset_y = 0
        end
      elsif ox > 0
        h = oh
        w = nw * (oh / nh)
        offset_x = (ow - w) / 2
        offset_y = 0
      else
        w = ow
        h = nh * (ow / nw)
        offset_x = 0
        offset_y = 0
      end
    end
  else
    w = ow
    h = oh
    offset_x = 0
    offset_y = 0
  end

  # [margin-top, margin-left, image width, image height]
  out = [
    offset_y.ceil,
    offset_x.ceil,
    w.ceil,
    h.ceil
  ]

  return out
end