Skip to content

Commit 50b7d0c

Browse files
authored
Merge pull request #32 from creativecirclemedia/optional-colorspace
Make -colorspace argument optional
2 parents edcdb51 + a4b3aea commit 50b7d0c

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

lib/grim/image_magick_processor.rb

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ def count(path)
2323
end
2424

2525
def save(pdf, index, path, options)
26+
command = prepare_command(pdf, index, path, options)
27+
command_env = {}
28+
29+
if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
30+
command_env['PATH'] = "#{File.dirname(@ghostscript_path)}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
31+
end
32+
33+
Grim.logger.debug { "Running imagemagick command" }
34+
if command_env.any?
35+
Grim.logger.debug { command_env.map {|k,v| "#{k}=#{v}" }.join(" ") }
36+
end
37+
Grim.logger.debug { command.join(" ") }
38+
39+
result, status = Open3.capture2e(command_env, command.join(" "))
40+
41+
status.success? || raise(UnprocessablePage, result)
42+
end
43+
44+
def prepare_command(pdf, index, path, options)
2645
width = options.fetch(:width, Grim::WIDTH)
2746
density = options.fetch(:density, Grim::DENSITY)
2847
quality = options.fetch(:quality, Grim::QUALITY)
@@ -36,27 +55,13 @@ def save(pdf, index, path, options)
3655
command << "-antialias"
3756
command << "-render"
3857
command << "-quality #{quality}"
39-
command << "-colorspace #{colorspace}"
58+
command << "-colorspace #{colorspace}" unless colorspace.nil?
4059
command << "-interlace none"
4160
command << "-density #{density}"
4261
command << "#{Shellwords.shellescape(pdf.path)}[#{index}]"
4362
command << path
4463

45-
command_env = {}
46-
47-
if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
48-
command_env['PATH'] = "#{File.dirname(@ghostscript_path)}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
49-
end
50-
51-
Grim.logger.debug { "Running imagemagick command" }
52-
if command_env.any?
53-
Grim.logger.debug { command_env.map {|k,v| "#{k}=#{v}" }.join(" ") }
54-
end
55-
Grim.logger.debug { command.join(" ") }
56-
57-
result, status = Open3.capture2e(command_env, command.join(" "))
58-
59-
status.success? || raise(UnprocessablePage, result)
64+
command
6065
end
6166
end
6267
end

spec/lib/grim/image_magick_processor_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,17 @@
128128
expect(`convert #{@path2} -verbose info:`.downcase.include?("alpha")).to be(false)
129129
end
130130
end
131+
132+
describe "#prepare_command" do
133+
before(:each) do
134+
@path = tmp_path("to_png_spec.jpg")
135+
@pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
136+
end
137+
138+
it "removes -colorspace if colorspace option is nil" do
139+
processor = Grim::ImageMagickProcessor.new
140+
expect(processor.prepare_command(@pdf, 0, @path, {:colorspace => nil}).join(" ")).not_to \
141+
match(/-colorspace/)
142+
end
143+
end
131144
end

0 commit comments

Comments
 (0)