Class: RackResize::Processors::Sips

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

Instance Method Summary collapse

Instance Method Details

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



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

def resize(source_file:, target_width:, target_height:, target_file: nil)
  # args = ["sips", "--deleteColorManagementProperties", "--optimizeColorForSharing"]
  args = ["sips", "--deleteColorManagementProperties", "--debug"]
  args += ["-s formatOptions", RackResize.config.default_quality]
  args << "--resampleWidth" << target_width.to_i if target_width
  args << "--resampleHeight" << target_height.to_i if target_height

  if target_file
    args << "-o" << Shellwords.escape(target_file)
  else
    tmp_file = Tempfile.new(["result", File.extname(source_file)])
    args << "-o" << tmp_file.path
  end

  args << Shellwords.escape(source_file)

  pp [:args, args, args.join(" ")]

  result = `#{args.join(" ")}`

  puts "sips command result: #{result}"

  unless target_file
    begin
      return File.open(tmp_file.path, 'rb', &:read)
    ensure
      tmp_file.unlink
    end
  end

  nil
end