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
|
# File 'lib/rack_resize/processors/sips.rb', line 6
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
args = %w[sips --deleteColorManagementProperties]
args += ["-s", "format", sips_format(format)] if format
args += ["-s", "formatOptions", quality.to_s]
if target_width && target_height
src_w, src_h = sips_dimensions(source_file)
if cover
if src_w.nil? || src_h.nil?
args += ["--resampleWidth", target_width.to_i.to_s]
elsif target_width.to_f / src_w >= target_height.to_f / src_h
args += ["--resampleWidth", target_width.to_i.to_s]
else
args += ["--resampleHeight", target_height.to_i.to_s]
end
elsif src_w && src_h
if (target_width.to_f / src_w) <= (target_height.to_f / src_h)
args += ["--resampleWidth", target_width.to_i.to_s]
else
args += ["--resampleHeight", target_height.to_i.to_s]
end
end
else
args += ["--resampleWidth", target_width.to_i.to_s] if target_width
args += ["--resampleHeight", target_height.to_i.to_s] if target_height
end
if target_file
run_sips(*args, "-o", target_file.to_s, source_file.to_s)
sips_crop!(target_file.to_s, target_width, target_height) if cover
nil
else
tmp = Tempfile.new(["result", File.extname(source_file)])
begin
run_sips(*args, "-o", tmp.path, source_file.to_s)
sips_crop!(tmp.path, target_width, target_height) if cover
File.binread(tmp.path)
ensure
tmp.close
tmp.unlink
end
end
end
|