Skip to content

Commit e9fdb49

Browse files
committed
Merge pull request #24 from Rubikan/master
added the possibility to use imagemagick's -alpha option
2 parents 874ea89 + ee67cd2 commit e9fdb49

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

lib/grim/image_magick_processor.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ImageMagickProcessor
99
def initialize(options={})
1010
@imagemagick_path = options[:imagemagick_path] || DefaultImagemagickPath
1111
@ghostscript_path = options[:ghostscript_path] || DefaultGhostScriptPath
12-
@original_path = ENV['PATH']
12+
@original_path = ENV['PATH']
1313
end
1414

1515
def count(path)
@@ -21,14 +21,25 @@ def count(path)
2121
end
2222

2323
def save(pdf, index, path, options)
24-
width = options.fetch(:width, Grim::WIDTH)
25-
density = options.fetch(:density, Grim::DENSITY)
26-
quality = options.fetch(:quality, Grim::QUALITY)
24+
width = options.fetch(:width, Grim::WIDTH)
25+
density = options.fetch(:density, Grim::DENSITY)
26+
quality = options.fetch(:quality, Grim::QUALITY)
2727
colorspace = options.fetch(:colorspace, Grim::COLORSPACE)
28-
command = [@imagemagick_path, "-resize", width.to_s, "-antialias", "-render",
29-
"-quality", quality.to_s, "-colorspace", colorspace,
30-
"-interlace", "none", "-density", density.to_s,
31-
"#{Shellwords.shellescape(pdf.path)}[#{index}]", path]
28+
alpha = options[:alpha]
29+
30+
command = []
31+
command << @imagemagick_path
32+
command << "-resize #{width}"
33+
command << "-alpha #{alpha}" if alpha
34+
command << "-antialias"
35+
command << "-render"
36+
command << "-quality #{quality}"
37+
command << "-colorspace #{colorspace}"
38+
command << "-interlace none"
39+
command << "-density #{density}"
40+
command << "#{Shellwords.shellescape(pdf.path)}[#{index}]"
41+
command << path
42+
3243
command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
3344

3445
result = `#{command.join(' ')}`

spec/fixtures/remove_alpha.pdf

31.7 KB
Binary file not shown.

spec/lib/grim/image_magick_processor_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,20 @@
112112
expect(file1_size).to_not eq(file2_size)
113113
end
114114
end
115+
116+
describe "#save with alpha option" do
117+
before(:each) do
118+
@path1 = tmp_path("to_png_spec-1.png")
119+
@path2 = tmp_path("to_png_spec-2.png")
120+
@pdf = Grim::Pdf.new(fixture_path("remove_alpha.pdf"))
121+
end
122+
123+
it "should use alpha" do
124+
Grim::ImageMagickProcessor.new.save(@pdf, 0, @path1, {:alpha => 'Set'})
125+
Grim::ImageMagickProcessor.new.save(@pdf, 0, @path2, {:alpha => 'Remove'})
126+
127+
expect(`convert #{@path1} -verbose info:`.include?("alpha: 8-bit")).to be(true)
128+
expect(`convert #{@path2} -verbose info:`.include?("alpha: 1-bit")).to be(true)
129+
end
130+
end
115131
end

0 commit comments

Comments
 (0)