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
|
# File 'lib/rack_resize/processors/mini_magick.rb', line 9
def resize(source_file:, target_width:, target_height:, target_file: nil, 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::MiniMagick.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
image = image.background(RackResize::ColorUtils.to_hex(bg_color)).flatten
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
|