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
if FileHelper.is_image?(filename)
begin
# fix orientation first
Upload.fix_image_orientation(file.path)
# retrieve image info
image_info = FastImage.new(file, raise_on_failure: true)
# compute image aspect ratio
@ -115,6 +117,10 @@ class Upload < ActiveRecord::Base
Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url)
end
def self.fix_image_orientation(path)
`convert #{path} -auto-orient #{path}`
end
end
# == Schema Information

View File

@ -16,7 +16,7 @@ describe AvatarUploadService do
let(:avatar_file) { AvatarUploadService.new(file, :image) }
it "should have a filesize" do
avatar_file.filesize.should == 2290
avatar_file.filesize.should > 0
end
it "should have a filename" do
@ -38,7 +38,7 @@ describe AvatarUploadService do
before { FileHelper.stubs(:download).returns(logo) }
it "should have a filesize" do
avatar_file.filesize.should == 2290
avatar_file.filesize.should > 0
end
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
before { Upload.stubs(:fix_image_orientation) }
it "does not create another upload if it already exists" do
Upload.expects(:find_by).with(sha1: image_sha1).returns(upload)
Upload.expects(:save).never
Upload.create_for(user_id, image, image_filename, image_filesize).should == upload
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
FastImage.any_instance.expects(:size).returns([100, 200])
ImageSizer.expects(:resize)