Skip to content

Commit

Permalink
Fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
EricR committed Jan 16, 2013
2 parents 1039c99 + 9401acc commit 28b10e4
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
.gem
Gemfile.lock
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.*
21 changes: 21 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ image.manipulate!(scale: "50%") # => true
image.dimensions # => { x: 250, y: 250 }
image.convert("thumbnail.jpg", quality: 80, crop: "100x100>") # => true
```

```ruby
image = Sorcery.new("multi-page.pdf")
image.filename_changed? # => false
image.manipulate!(format: "png", layer: 0) # => true
image.filename_changed? # => true
image.file # => "multi-page.png"
```

```ruby
image = Sorcery.new("multi-page.pdf")
image.manipulate!(format: "png") # => true
image.filename_changed? # => true

# on ImageMagick it returns all layers as a single file
image.file # => "multi-page-*.png"

# on GrapicksMagick it returns only the fist layer
image.file # => "multi-page.png"
```

# Using GraphicsMagick
Assuming you have GraphicsMagick installed on your box:

Expand Down
36 changes: 36 additions & 0 deletions lib/image_sorcery.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'gm_support'

class ImageSorcery
attr_reader :file

def initialize(file)
@file = file
end
Expand All @@ -15,6 +17,7 @@ def manipulate!(args={})
tokens << " -annotate #{args[:annotate].to_s}" if args[:annotate]
tokens = convert_to_command(tokens)
success = run(tokens)[1]
replace_file(args[:format].to_s.downcase, args[:layer]) if success && args[:format]
success
end

Expand Down Expand Up @@ -75,8 +78,41 @@ def montage(sources, args={})
success = run(tokens)[1]
end

def filename_changed?
(@filename_changed)
end

private

# Replaces the old file (with the old file format) with the newly generated one.
# The old file will be deleted and $file will be reset.
# If ImageMagick generated more than one file, $file will have a "*", so that all files generated
# will be manipulated in the following steps.
def replace_file(format, layer)
return if File.extname(@file) == format

layer ||= 0
layer = layer.split(",").first if layer.is_a? String

File.delete @file
@filename_changed = true

path = File.join File.dirname(@file), File.basename(@file, File.extname(@file))
new_file = find_file path, format, layer

@file = new_file.call(path, format, "*") unless new_file.nil?
end

def find_file(path, format, layer)
possible_paths = [
Proc.new { |path, format, layer| "#{path}.#{format}" },
Proc.new { |path, format, layer| "#{path}-#{layer}.#{format}" },
Proc.new { |path, format, layer| "#{path}.#{format}.#{layer}" }
]

possible_paths.find { |possible_path| File.exists?(possible_path.call(path, format, layer)) }
end

def convert_to_command(tokens)
tokens[0] = prefix(tokens[0]) if respond_to? :prefix
tokens.flatten.join("")
Expand Down
Binary file removed spec/fixtures/dog-2.jpeg
Binary file not shown.
Binary file added spec/fixtures/pdf-sample.pdf
Binary file not shown.
108 changes: 99 additions & 9 deletions spec/image_sorcery_examples_spec.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,127 @@
require 'fileutils'
require 'image_sorcery'

shared_examples_for ImageSorcery do
shared_examples_for ImageSorcery do |new_instance_method|
subject(:image) do
FileUtils.copy "./spec/fixtures/dog.jpeg", "./spec/fixtures/dog-2.jpeg"
ImageSorcery.send(new_instance_method, "./spec/fixtures/dog-2.jpeg") # Who doesn't love dogs?
end

after :each do
[
"new_image.png",
"./spec/fixtures/dog-2.jpeg",
"./spec/fixtures/dog-2.png",
"./spec/fixtures/pdf-sample-2.pdf",
"./spec/fixtures/pdf-sample-2.png",
"./spec/fixtures/pdf-sample-2-0.png",
"./spec/fixtures/pdf-sample-2-1.png",
"./spec/fixtures/pdf-sample-2.png.0",
"./spec/fixtures/pdf-sample-2.png.1"
].each do |file|
File.exists?(file) && File.delete(file)
end
end

describe "getting the dimensions of an image" do
it "returns a hash of dimensions" do
@image.dimensions.should == {:x => 160, :y => 120}
image.dimensions.should == {:x => 160, :y => 120}
end
end

describe "manipulating an image" do
it "resizes an image" do
original_dimensions = @image.dimensions
@image.manipulate!(:resize => "50%")
@image.dimensions.map {|k,v| v.to_i}.should == original_dimensions.map {|k,v| v.to_i/2}
original_dimensions = image.dimensions
image.manipulate!(:resize => "50%")
image.dimensions.map {|k,v| v.to_i}.should == original_dimensions.map {|k,v| v.to_i/2}
end

it "exposes width and height as integers" do
@image.width.should == 160
@image.height.should == 120
image.width.should == 160
image.height.should == 120
end

its(:filename_changed?) { should be_false }

its(:file) { should eq "./spec/fixtures/dog-2.jpeg" }

describe "change of format" do
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

it "should create file with new extension" do
File.exists?("./spec/fixtures/dog-2.png").should be_true
end

its(:identify) { should include "PNG 160x120" }

its(:filename_changed?) { should be_true }

its(:file) { should eq "./spec/fixtures/dog-2.png" }
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

subject :image do
FileUtils.copy "./spec/fixtures/pdf-sample.pdf", "./spec/fixtures/pdf-sample-2.pdf"
ImageSorcery.send(new_instance_method, "./spec/fixtures/pdf-sample-2.pdf")
end

it "should delete original file" do
File.exists?("./spec/fixtures/pdf-sample-2.pdf").should be_false
end

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

its(:identify) { should include "PNG 595x842" }

its(:filename_changed?) { should be_true }

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

describe "converting an image" do
it "writes the new image out to a file" do
@image.convert("new_image.png")
image.convert("new_image.png")
File.exists?("new_image.png").should be_true
end
end

describe "identifying an image" do
it "returns a list of layers" do
@image.identify.should include "JPEG 160x120"
image.identify.should include "JPEG 160x120"
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/image_sorcery_gm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@image = ImageSorcery.gm("./spec/fixtures/dog-2.jpeg") # Who doesn't love dogs?
end

it_behaves_like ImageSorcery
it_behaves_like ImageSorcery, "gm"
end

0 comments on commit 28b10e4

Please sign in to comment.