Skip to content

Commit 9932790

Browse files
committed
added the possibility to use imagemagick's -alpha option
1 parent 874ea89 commit 9932790

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

lib/grim.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module Grim
1616

1717
# Default colorspace
1818
COLORSPACE = "RGB"
19+
20+
#Default Alpha
21+
ALPHA = "Set"
1922

2023
# Default exception class for Grim.
2124
class Exception < ::StandardError

lib/grim/image_magick_processor.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def save(pdf, index, path, options)
2525
density = options.fetch(:density, Grim::DENSITY)
2626
quality = options.fetch(:quality, Grim::QUALITY)
2727
colorspace = options.fetch(:colorspace, Grim::COLORSPACE)
28-
command = [@imagemagick_path, "-resize", width.to_s, "-antialias", "-render",
28+
alpha = options.fetch(:alpha, Grim::ALPHA)
29+
command = [@imagemagick_path, "-resize", width.to_s, "-alpha", alpha, "-antialias", "-render",
2930
"-quality", quality.to_s, "-colorspace", colorspace,
3031
"-interlace", "none", "-density", density.to_s,
3132
"#{Shellwords.shellescape(pdf.path)}[#{index}]", path]

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

spec/lib/grim_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
it "should have COLORSPACE constant set to 'RGB'" do
2626
expect(Grim::COLORSPACE).to eq('RGB')
2727
end
28+
29+
it "should have ALPHA constant set to 'Set'" do
30+
expect(Grim::ALPHA).to eq('Set')
31+
end
2832

2933
describe "#reap" do
3034
it "should return an instance of Grim::Pdf" do

0 commit comments

Comments
 (0)