Class: RackResize::Processors::MiniMagick

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

Overview

require 'mini_magick' MiniMagick.logger.level = :debug

Instance Method Summary collapse

Instance Method Details

#resize(source_file:, target_width:, target_height:, target_file: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rack_resize/processors/mini_magick.rb', line 12

def resize(source_file:, target_width:, target_height:, target_file: nil)
  image = ImageProcessing::MiniMagick.source(source_file)
  image = image.resize_to_limit(target_width, target_height) if target_width || target_height
  image = image.saver(quality: RackResize.config.default_quality)

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

#resize_mm(source_file:, target_width:, target_height:, target_file: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack_resize/processors/mini_magick.rb', line 29

def resize_mm(source_file:, target_width:, target_height:, target_file: nil)
  image = MiniMagick::Image.open(source_file)
  image.combine_options do |img|
    img.resize("#{target_width}x#{target_height}>") if target_width || target_height
    img.quality(RackResize.config.default_quality)

    if target_file
      img.write(target_file)
    end
  end

  unless target_file
    return File.open(image.path, 'rb', &:read)
  end

ensure
  image&.destroy!
end