Class: Defile::ImageProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/defile/image_processing.rb

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.



6
7
8
# File 'lib/defile/image_processing.rb', line 6

def initialize(method)
  @method = method
end

Instance Method Details

#call(file, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/defile/image_processing.rb', line 59

def call(file, *args)
  path = file.download.path
  img = ::MiniMagick::Image.open(path)
  send(@method, img, *args)

  img.write(path)

  ::File.open(path, "rb")
end

#convert(img, format) ⇒ Object



10
11
12
# File 'lib/defile/image_processing.rb', line 10

def convert(img, format)
  img.format(format.to_s.downcase)
end

#fill(img, width, height, gravity = 'Center') ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/defile/image_processing.rb', line 22

def fill(img, width, height, gravity = 'Center')
  width = width.to_i
  height = height.to_i
  cols, rows = img[:dimensions]
  img.combine_options do |cmd|
    if width != cols || height != rows
      scale_x = width/cols.to_f
      scale_y = height/rows.to_f
      if scale_x >= scale_y
        cols = (scale_x * (cols + 0.5)).round
        rows = (scale_x * (rows + 0.5)).round
        cmd.resize "#{cols}"
      else
        cols = (scale_y * (cols + 0.5)).round
        rows = (scale_y * (rows + 0.5)).round
        cmd.resize "x#{rows}"
      end
    end
    cmd.gravity gravity
    cmd.background "rgba(255,255,255,0.0)"
    cmd.extent "#{width}x#{height}" if cols != width || rows != height
  end
end

#fit(img, width, height) ⇒ Object



18
19
20
# File 'lib/defile/image_processing.rb', line 18

def fit(img, width, height)
  img.resize "#{width}x#{height}"
end

#limit(img, width, height) ⇒ Object



14
15
16
# File 'lib/defile/image_processing.rb', line 14

def limit(img, width, height)
  img.resize "#{width}x#{height}>"
end

#pad(img, width, height, background = "transparent", gravity = "Center") ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/defile/image_processing.rb', line 46

def pad(img, width, height, background = "transparent", gravity = "Center")
  img.combine_options do |cmd|
    cmd.thumbnail "#{width}x#{height}>"
    if background == "transparent"
      cmd.background "rgba(255, 255, 255, 0.0)"
    else
      cmd.background background
    end
    cmd.gravity gravity
    cmd.extent "#{width}x#{height}"
  end
end