FEATURE: automatic image orientation fix

This commit is contained in:
Régis Hanol 2014-07-09 23:59:57 +02:00
parent 362adb0498
commit a52c80e2a8
5 changed files with 15 additions and 2 deletions

View file

@ -75,6 +75,8 @@ class Upload < ActiveRecord::Base
# deal with width & height for images # deal with width & height for images
if FileHelper.is_image?(filename) if FileHelper.is_image?(filename)
begin begin
# fix orientation first
Upload.fix_image_orientation(file.path)
# retrieve image info # retrieve image info
image_info = FastImage.new(file, raise_on_failure: true) image_info = FastImage.new(file, raise_on_failure: true)
# compute image aspect ratio # compute image aspect ratio
@ -115,6 +117,10 @@ class Upload < ActiveRecord::Base
Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url) Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url)
end end
def self.fix_image_orientation(path)
`convert #{path} -auto-orient #{path}`
end
end end
# == Schema Information # == Schema Information

View file

@ -16,7 +16,7 @@ describe AvatarUploadService do
let(:avatar_file) { AvatarUploadService.new(file, :image) } let(:avatar_file) { AvatarUploadService.new(file, :image) }
it "should have a filesize" do it "should have a filesize" do
avatar_file.filesize.should == 2290 avatar_file.filesize.should > 0
end end
it "should have a filename" do it "should have a filename" do
@ -38,7 +38,7 @@ describe AvatarUploadService do
before { FileHelper.stubs(:download).returns(logo) } before { FileHelper.stubs(:download).returns(logo) }
it "should have a filesize" do it "should have a filesize" do
avatar_file.filesize.should == 2290 avatar_file.filesize.should > 0
end end
it "should have a filename" do it "should have a filename" do

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -48,12 +48,19 @@ describe Upload do
context "#create_for" do context "#create_for" do
before { Upload.stubs(:fix_image_orientation) }
it "does not create another upload if it already exists" do it "does not create another upload if it already exists" do
Upload.expects(:find_by).with(sha1: image_sha1).returns(upload) Upload.expects(:find_by).with(sha1: image_sha1).returns(upload)
Upload.expects(:save).never Upload.expects(:save).never
Upload.create_for(user_id, image, image_filename, image_filesize).should == upload Upload.create_for(user_id, image, image_filename, image_filesize).should == upload
end end
it "fix image orientation" do
Upload.expects(:fix_image_orientation).with(image.path)
Upload.create_for(user_id, image, image_filename, image_filesize)
end
it "computes width & height for images" do it "computes width & height for images" do
FastImage.any_instance.expects(:size).returns([100, 200]) FastImage.any_instance.expects(:size).returns([100, 200])
ImageSizer.expects(:resize) ImageSizer.expects(:resize)