diff --git a/app/models/upload.rb b/app/models/upload.rb index 03a965306c2..f01c6442467 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -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 diff --git a/spec/components/avatar_upload_service_spec.rb b/spec/components/avatar_upload_service_spec.rb index c515bbb3450..d33a2fab402 100644 --- a/spec/components/avatar_upload_service_spec.rb +++ b/spec/components/avatar_upload_service_spec.rb @@ -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 diff --git a/spec/fixtures/images/logo-dev.png b/spec/fixtures/images/logo-dev.png index 1b309fc3755..90aaf71f981 100644 Binary files a/spec/fixtures/images/logo-dev.png and b/spec/fixtures/images/logo-dev.png differ diff --git a/spec/fixtures/images/logo.png b/spec/fixtures/images/logo.png index c8d7600f7ab..6fa34467aae 100644 Binary files a/spec/fixtures/images/logo.png and b/spec/fixtures/images/logo.png differ diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 8b4224116c6..545d6501cf9 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -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)