Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
auto-orient option added
Browse files Browse the repository at this point in the history
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
Johannes Barre authored and Jon Yurek committed Aug 24, 2012
1 parent 5161c5f commit 4bea897
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/paperclip/thumbnail.rb
Expand Up @@ -3,7 +3,7 @@ module Paperclip
class Thumbnail < Processor class Thumbnail < Processor


attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, 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 # List of formats that we need to preserve animation
ANIMATED_FORMATS = %w(gif) ANIMATED_FORMATS = %w(gif)
Expand Down Expand Up @@ -38,6 +38,7 @@ def initialize(file, options = {}, attachment = nil)
@whiny = options[:whiny].nil? ? true : options[:whiny] @whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format] @format = options[:format]
@animated = options[:animated].nil? ? true : options[:animated] @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) @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) @convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
Expand Down Expand Up @@ -89,6 +90,7 @@ def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?) scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = [] trans = []
trans << "-coalesce" if animated? trans << "-coalesce" if animated?
trans << "-auto-orient" if auto_orient
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty? trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
trans << "-crop" << %["#{crop}"] << "+repage" if crop trans << "-crop" << %["#{crop}"] << "+repage" if crop
trans trans
Expand Down
Binary file added test/fixtures/rotated.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 59 additions & 3 deletions test/thumbnail_test.rb
Expand Up @@ -114,7 +114,7 @@ class ThumbnailTest < Test::Unit::TestCase


should "send the right command to convert when sent #make" do should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg| @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]" arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end end
@thumb.make @thumb.make
Expand All @@ -139,7 +139,7 @@ class ThumbnailTest < Test::Unit::TestCase


should "send the right command to convert when sent #make" do should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg| @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]" arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end end
@thumb.make @thumb.make
Expand Down Expand Up @@ -180,7 +180,7 @@ class ThumbnailTest < Test::Unit::TestCase


should "send the right command to convert when sent #make" do should "send the right command to convert when sent #make" do
@thumb.expects(:convert).with do |*arg| @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]" arg[1][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end end
@thumb.make @thumb.make
Expand Down Expand Up @@ -302,6 +302,62 @@ def to_s
end end
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 context "A multipage PDF" do
setup do setup do
@file = File.new(fixture_file("twopage.pdf"), 'rb') @file = File.new(fixture_file("twopage.pdf"), 'rb')
Expand Down

0 comments on commit 4bea897

Please sign in to comment.