Commit 4bea897f by Johannes Barre Committed by Jon Yurek

auto-orient option added

This adds a auto-orient option, which is enabled by default. I believe, most users expect their images to be rotated according to EXIF. The photo I've added to the fixtures was token by myself, I release it to the public domain.
parent 5161c5fa
......@@ -3,7 +3,7 @@ module Paperclip
class Thumbnail < Processor
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options,
:source_file_options, :animated
:source_file_options, :animated, :auto_orient
# List of formats that we need to preserve animation
ANIMATED_FORMATS = %w(gif)
......@@ -38,6 +38,7 @@ module Paperclip
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
@animated = options[:animated].nil? ? true : options[:animated]
@auto_orient = options[:auto_orient].nil? ? true : options[:auto_orient]
@source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
@convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
......@@ -89,6 +90,7 @@ module Paperclip
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = []
trans << "-coalesce" if animated?
trans << "-auto-orient" if auto_orient
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
trans << "-crop" << %["#{crop}"] << "+repage" if crop
trans
......
......@@ -114,7 +114,7 @@ class ThumbnailTest < Test::Unit::TestCase
should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg|
arg[0] == ':source -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[0] == ':source -auto-orient -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
......@@ -139,7 +139,7 @@ class ThumbnailTest < Test::Unit::TestCase
should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg|
arg[0] == '-strip :source -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[0] == '-strip :source -auto-orient -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
......@@ -180,7 +180,7 @@ class ThumbnailTest < Test::Unit::TestCase
should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg|
arg[0] == ':source -resize "x50" -crop "100x50+114+0" +repage -strip -depth 8 :dest' &&
arg[0] == ':source -auto-orient -resize "x50" -crop "100x50+114+0" +repage -strip -depth 8 :dest' &&
arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
......@@ -302,6 +302,62 @@ class ThumbnailTest < Test::Unit::TestCase
end
end
context "An image with exif orientation" do
setup do
@file = File.new(fixture_file("rotated.jpg"), 'rb')
end
teardown { @file.close }
context "With :auto_orient => false" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50", :auto_orient => false)
end
should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg|
arg[0] == ':source -resize "100x50" :dest' &&
arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
end
should "create the thumbnail when sent #make" do
dst = @thumb.make
assert_match /75x50/, `identify "#{dst.path}"`
end
should "not touch the orientation information" do
dst = @thumb.make
assert_match /exif:Orientation=6/, `identify -format "%[EXIF:*]" "#{dst.path}"`
end
end
context "Without :auto_orient => false" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50")
end
should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg|
arg[0] == ':source -auto-orient -resize "100x50" :dest' &&
arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
end
should "create the thumbnail when sent #make" do
dst = @thumb.make
assert_match /33x50/, `identify "#{dst.path}"`
end
should "remove the orientation information" do
dst = @thumb.make
assert_match /exif:Orientation=1/, `identify -format "%[EXIF:*]" "#{dst.path}"`
end
end
end
context "A multipage PDF" do
setup do
@file = File.new(fixture_file("twopage.pdf"), 'rb')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment