mloughran (owner)

Revisions

gist: 65818 Download_button fork
public
Public Clone URL: git://gist.github.com/65818.git
Embed All Files: show embed
Ruby #
1
2
3
4
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
module IrregularScience
  def resize_within(w, h)
    r_old = width.to_f / height
    r_new = w.to_f / h
  
    w_new = r_new > r_old ? (h * r_old).to_i : w
    h_new = r_new > r_old ? h : (w / r_old).to_i
    
    self.resize(w_new, h_new) do |image|
      yield image
    end
  end
 
  def resize_exact(w, h)
    r_old = width.to_f / height
    r_new = w.to_f / h
  
    w_crop = r_new > r_old ? width : (height * r_new).to_i
    h_crop = r_new > r_old ? (width / r_new).to_i : height
  
    trim_w = (width - w_crop) / 2
    trim_h = (height - h_crop) / 2
  
    l, r = trim_w, trim_w + w_crop
    t, b = trim_h, trim_h + h_crop
    
    self.with_crop(l, t, r, b) do |img|
      img.resize(w, h) do |thumb|
        yield thumb
      end
    end
  end
 
  def resize_to_width(w)
    scale = w.to_f / width
  
    img.resize(w, h*scale) do |image|
      yield image
    end
  end
 
  def resize_to_height(h)
    scale = h.to_f / height
  
    img.resize(w*scale, h) do |image|
      yield image
    end
  end
end
 
ImageScience.send(:include, IrregularScience)