Class: RackResize::Processors::Vips

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_resize/processors/vips.rb

Instance Method Summary collapse

Instance Method Details

#resize(source_file:, target_file:, target_width:, target_height:, fit: nil, format: nil, quality: nil, bg_color: nil) ⇒ Object



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
# File 'lib/rack_resize/processors/vips.rb', line 9

def resize(source_file:, target_file:, target_width:, target_height:, fit: nil, format: nil, quality: nil, bg_color: nil)
  quality ||= RackResize.config.default_quality
  cover = (fit == 'cover' || fit == 'crop') && target_width && target_height

  image = ImageProcessing::Vips.source(source_file)
  image = image.convert(format) if format

  if target_width || target_height
    image = cover ? image.resize_to_fill(target_width, target_height)
                  : image.resize_to_limit(target_width, target_height)
  end

  if bg_color
    r, g, b, _a = bg_color
    image = image.flatten(background: [r, g, b])
  end

  image = image.saver(quality: quality)

  if target_file
    image.call(destination: target_file)
    nil
  else
    begin
      tmp_file = image.call
      return tmp_file.read
    ensure
      tmp_file&.unlink
    end
  end
end