Skip to content

Commit

Permalink
Auto-orient images when determining dimensions
Browse files Browse the repository at this point in the history
libvips and image magick handle image orientation correctly when
creating thumbnails. Paperclip only parses the orientation header, but
does not automatically apply when parsing geometry.

Storing dimensions in the wrong orientation causes inline images to
have an incorrect aspect ratio.

REDMINE-20746
  • Loading branch information
tf committed Jun 14, 2024
1 parent cf5c162 commit 22a33d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/pageflow/image_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def self.scale_down_to_cover(width, height)

def save_image_dimensions
geo = Paperclip::Geometry.from_file(attachment.queued_for_write[:original])
geo.auto_orient

self.width = geo.width
self.height = geo.height
rescue Paperclip::Errors::NotIdentifiedByImageMagickError
Expand Down
Binary file added spec/fixtures/7x15_rotated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ module Pageflow
expect(file.reload.height).to eq(15)
end

it 'auto orients image when saving width and heigh' do
file = create(:image_file, :uploaded, file_name: '7x15_rotated.jpg')

file.process

expect(file.reload.width).to eq(15)
expect(file.reload.height).to eq(7)
end

it 'sets width and height to nil if image cannot be identified' do
file = create(:image_file, :uploaded, file_name: 'broken.jpg')

Expand Down

0 comments on commit 22a33d3

Please sign in to comment.