Permalink
Browse files

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.
  • Loading branch information...
1 parent 5161c5f commit 4bea897fa277eb3928442826e1098108590d9b0b Johannes Barre committed with jyurek Aug 15, 2012
Showing with 62 additions and 4 deletions.
  1. +3 −1 lib/paperclip/thumbnail.rb
  2. BIN test/fixtures/rotated.jpg
  3. +59 −3 test/thumbnail_test.rb
@@ -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 @@ def initialize(file, options = {}, attachment = nil)
@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 @@ def transformation_command
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
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -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 @@ def to_s
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')

0 comments on commit 4bea897

Please sign in to comment.