Permalink
Browse files

Fix for inconsistent behaviour of GraphicsMagick

It also handles the case when `:layer => "0,1,5"`
  • Loading branch information...
1 parent 46a9545 commit 3b5b7c0f2cccdb81d16519c8d4831823dd0026a9 @tmaier tmaier committed Dec 30, 2012
Showing with 55 additions and 29 deletions.
  1. +1 −0 .gitignore
  2. +15 −5 lib/image_sorcery.rb
  3. +39 −24 spec/image_sorcery_examples_spec.rb
View
@@ -6,3 +6,4 @@ new_image.png
spec/fixtures/dog-2.jpeg
spec/fixtures/dog-2.png
spec/fixtures/pdf-sample-2*.png
+spec/fixtures/pdf-sample-2.png.*
View
@@ -18,7 +18,7 @@ def manipulate!(args={})
tokens = convert_to_command(tokens)
success = run(tokens)[1]
if success && args[:format]
- replace_file args[:format].to_s.downcase
+ replace_file args[:format].to_s.downcase, args[:layer]
end
success
end
@@ -87,15 +87,25 @@ def filename_changed?
private
- def replace_file(format)
+ def replace_file(format, layer)
return if File.extname(@file) == format
+
+ layer ||= 0
+ layer = layer.split(",").first if layer.is_a? String
+ file_path = File.join File.dirname(@file), File.basename(@file, File.extname(@file))
+
File.delete @file
- @file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "." + format
@filename_changed = true
- unless File.exists? @file
- @file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "-*." + format
+ possible_paths = [
+ Proc.new { |file_path, format, layer| "#{file_path}.#{format}" },
+ Proc.new { |file_path, format, layer| "#{file_path}-#{layer}.#{format}" },
+ Proc.new { |file_path, format, layer| "#{file_path}.#{format}.#{layer}" }
+ ]
+ until possible_paths.empty? || File.exists?(possible_paths.first.call(file_path, format, layer))
+ possible_paths.shift
end
+ @file = possible_paths.first.call(file_path, format, "*") unless possible_paths.empty?
end
def convert_to_command(tokens)
@@ -46,11 +46,11 @@
its(:file) { should eq "./spec/fixtures/dog-2.jpeg" }
describe "change of format" do
- before :each do
- image.manipulate!(:format => "png")
- end
-
describe "with image" do
+ before :each do
+ image.manipulate!(:format => "png")
+ end
+
it "should delete original file" do
File.exists?("./spec/fixtures/dog-2.jpeg").should be_false
end
@@ -66,33 +66,48 @@
its(:file) { should eq "./spec/fixtures/dog-2.png" }
end
- describe "with multi page pdf" do
- subject :image do
- FileUtils.copy "./spec/fixtures/pdf-sample.pdf", "./spec/fixtures/pdf-sample-2.pdf"
- Sorcery.send(new_instance_method, "./spec/fixtures/pdf-sample-2.pdf")
- end
+ [nil, "0,1"].each do |layer|
+ describe "with multi page pdf and :layer => '#{layer}'" do
+ before :each do
+ image.manipulate!(:format => "png", :layer => layer)
+ end
- it "should delete original file" do
- File.exists?("./spec/fixtures/pdf-sample-2.pdf").should be_false
- end
+ subject :image do
+ FileUtils.copy "./spec/fixtures/pdf-sample.pdf", "./spec/fixtures/pdf-sample-2.pdf"
+ Sorcery.send(new_instance_method, "./spec/fixtures/pdf-sample-2.pdf")
+ end
- it "should create file with new extension" do
+ it "should delete original file" do
+ File.exists?("./spec/fixtures/pdf-sample-2.pdf").should be_false
+ end
- case new_instance_method
- when "new"
- File.exists?("./spec/fixtures/pdf-sample-2-0.png").should be_true
- File.exists?("./spec/fixtures/pdf-sample-2-1.png").should be_true
- when "gm"
- File.exists?("./spec/fixtures/pdf-sample-2.png").should be_true
+ it "should create file with new extension" do
+
+ case new_instance_method
+ when "new"
+ File.exists?("./spec/fixtures/pdf-sample-2-0.png").should be_true
+ File.exists?("./spec/fixtures/pdf-sample-2-1.png").should be_true
+ when "gm"
+ # Inconsistent behaviour of GraphicsMagick
+ case subject.file
+ when "./spec/fixtures/pdf-sample-2.png" # GraphicsMagick 1.3.17 2012-10-13
+ File.exists?("./spec/fixtures/pdf-sample-2.png").should be_true
+ when "./spec/fixtures/pdf-sample-2.png.*" # GraphicsMagick 1.3.12 2010-03-08
+ File.exists?("./spec/fixtures/pdf-sample-2.png.0").should be_true
+ File.exists?("./spec/fixtures/pdf-sample-2.png.1").should be_true
+ end
+ end
end
- end
- its(:identify) { should include "PNG 595x842" }
+ its(:identify) { should include "PNG 595x842" }
- its(:filename_changed?) { should be_true }
+ its(:filename_changed?) { should be_true }
- its(:file) { should eq "./spec/fixtures/pdf-sample-2.png" } if new_instance_method == "gm"
- its(:file) { should eq "./spec/fixtures/pdf-sample-2-*.png" } if new_instance_method == "new"
+ its(:file) { should eq "./spec/fixtures/pdf-sample-2-*.png" } if new_instance_method == "new"
+ # Commented out, because of inconsistent behaviour of GraphicsMagick
+ #its(:file) { should eq "./spec/fixtures/pdf-sample-2.png" } if new_instance_method == "gm"
+ #its(:file) { should eq "./spec/fixtures/pdf-sample-2.png.*" } if new_instance_method == "gm"
+ end
end
end
end

0 comments on commit 3b5b7c0

Please sign in to comment.