Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix for inconsistent behaviour of GraphicsMagick

It also handles the case when `:layer => "0,1,5"`
  • Loading branch information...
commit 3b5b7c0f2cccdb81d16519c8d4831823dd0026a9 1 parent 46a9545
Tobias L. Maier authored December 30, 2012
1  .gitignore
@@ -6,3 +6,4 @@ new_image.png
6 6
 spec/fixtures/dog-2.jpeg
7 7
 spec/fixtures/dog-2.png
8 8
 spec/fixtures/pdf-sample-2*.png
  9
+spec/fixtures/pdf-sample-2.png.*
20  lib/image_sorcery.rb
@@ -18,7 +18,7 @@ def manipulate!(args={})
18 18
     tokens  = convert_to_command(tokens)
19 19
     success = run(tokens)[1]
20 20
     if success && args[:format]
21  
-      replace_file args[:format].to_s.downcase
  21
+      replace_file args[:format].to_s.downcase, args[:layer]
22 22
     end
23 23
     success
24 24
   end
@@ -87,15 +87,25 @@ def filename_changed?
87 87
 
88 88
   private
89 89
 
90  
-  def replace_file(format)
  90
+  def replace_file(format, layer)
91 91
     return if  File.extname(@file) == format
  92
+
  93
+    layer ||= 0
  94
+    layer = layer.split(",").first if layer.is_a? String
  95
+    file_path = File.join File.dirname(@file), File.basename(@file, File.extname(@file))
  96
+
92 97
     File.delete @file
93  
-    @file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "." + format
94 98
     @filename_changed = true
95 99
 
96  
-    unless File.exists? @file
97  
-      @file = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) + "-*." + format
  100
+    possible_paths = [
  101
+        Proc.new { |file_path, format, layer| "#{file_path}.#{format}" },
  102
+        Proc.new { |file_path, format, layer| "#{file_path}-#{layer}.#{format}" },
  103
+        Proc.new { |file_path, format, layer| "#{file_path}.#{format}.#{layer}" }
  104
+    ]
  105
+    until possible_paths.empty? || File.exists?(possible_paths.first.call(file_path, format, layer))
  106
+      possible_paths.shift
98 107
     end
  108
+    @file = possible_paths.first.call(file_path, format, "*") unless possible_paths.empty?
99 109
   end
100 110
 
101 111
   def convert_to_command(tokens)
63  spec/image_sorcery_examples_spec.rb
@@ -46,11 +46,11 @@
46 46
     its(:file) { should eq "./spec/fixtures/dog-2.jpeg" }
47 47
 
48 48
     describe "change of format" do
49  
-      before :each do
50  
-        image.manipulate!(:format => "png")
51  
-      end
52  
-
53 49
       describe "with image" do
  50
+        before :each do
  51
+          image.manipulate!(:format => "png")
  52
+        end
  53
+
54 54
         it "should delete original file" do
55 55
           File.exists?("./spec/fixtures/dog-2.jpeg").should be_false
56 56
         end
@@ -66,33 +66,48 @@
66 66
         its(:file) { should eq "./spec/fixtures/dog-2.png" }
67 67
       end
68 68
 
69  
-      describe "with multi page pdf" do
70  
-        subject :image do
71  
-          FileUtils.copy "./spec/fixtures/pdf-sample.pdf", "./spec/fixtures/pdf-sample-2.pdf"
72  
-          Sorcery.send(new_instance_method, "./spec/fixtures/pdf-sample-2.pdf")
73  
-        end
  69
+      [nil, "0,1"].each do |layer|
  70
+        describe "with multi page pdf and :layer => '#{layer}'" do
  71
+          before :each do
  72
+            image.manipulate!(:format => "png", :layer => layer)
  73
+          end
74 74
 
75  
-        it "should delete original file" do
76  
-          File.exists?("./spec/fixtures/pdf-sample-2.pdf").should be_false
77  
-        end
  75
+          subject :image do
  76
+            FileUtils.copy "./spec/fixtures/pdf-sample.pdf", "./spec/fixtures/pdf-sample-2.pdf"
  77
+            Sorcery.send(new_instance_method, "./spec/fixtures/pdf-sample-2.pdf")
  78
+          end
78 79
 
79  
-        it "should create file with new extension" do
  80
+          it "should delete original file" do
  81
+            File.exists?("./spec/fixtures/pdf-sample-2.pdf").should be_false
  82
+          end
80 83
 
81  
-          case new_instance_method
82  
-            when "new"
83  
-              File.exists?("./spec/fixtures/pdf-sample-2-0.png").should be_true
84  
-              File.exists?("./spec/fixtures/pdf-sample-2-1.png").should be_true
85  
-            when "gm"
86  
-              File.exists?("./spec/fixtures/pdf-sample-2.png").should be_true
  84
+          it "should create file with new extension" do
  85
+
  86
+            case new_instance_method
  87
+              when "new"
  88
+                File.exists?("./spec/fixtures/pdf-sample-2-0.png").should be_true
  89
+                File.exists?("./spec/fixtures/pdf-sample-2-1.png").should be_true
  90
+              when "gm"
  91
+                # Inconsistent behaviour of GraphicsMagick
  92
+                case subject.file
  93
+                  when "./spec/fixtures/pdf-sample-2.png"   # GraphicsMagick 1.3.17 2012-10-13
  94
+                    File.exists?("./spec/fixtures/pdf-sample-2.png").should be_true
  95
+                  when "./spec/fixtures/pdf-sample-2.png.*" # GraphicsMagick 1.3.12 2010-03-08
  96
+                    File.exists?("./spec/fixtures/pdf-sample-2.png.0").should be_true
  97
+                    File.exists?("./spec/fixtures/pdf-sample-2.png.1").should be_true
  98
+                end
  99
+            end
87 100
           end
88  
-        end
89 101
 
90  
-        its(:identify) { should include "PNG 595x842" }
  102
+          its(:identify) { should include "PNG 595x842" }
91 103
 
92  
-        its(:filename_changed?) { should be_true }
  104
+          its(:filename_changed?) { should be_true }
93 105
 
94  
-        its(:file) { should eq "./spec/fixtures/pdf-sample-2.png" } if new_instance_method == "gm"
95  
-        its(:file) { should eq "./spec/fixtures/pdf-sample-2-*.png" } if new_instance_method == "new"
  106
+          its(:file) { should eq "./spec/fixtures/pdf-sample-2-*.png" } if new_instance_method == "new"
  107
+          # Commented out, because of inconsistent behaviour of GraphicsMagick
  108
+          #its(:file) { should eq "./spec/fixtures/pdf-sample-2.png" } if new_instance_method == "gm"
  109
+          #its(:file) { should eq "./spec/fixtures/pdf-sample-2.png.*" } if new_instance_method == "gm"
  110
+        end
96 111
       end
97 112
     end
98 113
   end

0 notes on commit 3b5b7c0

Please sign in to comment.
Something went wrong with that request. Please try again.