From f9b806eaa18c7bdaedb36a073a450f5fa6417d2e Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Wed, 31 Jan 2018 15:43:29 -0500 Subject: Swap encoded image width and height if angle is 90 or 270 degrees --- .../lib/active_storage/analyzer/image_analyzer.rb | 12 +++++++++++- activestorage/test/analyzer/image_analyzer_test.rb | 17 +++++++++++++++-- activestorage/test/fixtures/files/racecar_rotated.jpg | Bin 0 -> 1124060 bytes 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 activestorage/test/fixtures/files/racecar_rotated.jpg diff --git a/activestorage/lib/active_storage/analyzer/image_analyzer.rb b/activestorage/lib/active_storage/analyzer/image_analyzer.rb index 5231168a7c..7342178eff 100644 --- a/activestorage/lib/active_storage/analyzer/image_analyzer.rb +++ b/activestorage/lib/active_storage/analyzer/image_analyzer.rb @@ -3,6 +3,8 @@ module ActiveStorage # Extracts width and height in pixels from an image blob. # + # If the image contains EXIF data indicating its angle is 90 or 270 degrees, its width and height are swapped for convenience. + # # Example: # # ActiveStorage::Analyzer::ImageAnalyzer.new(blob).metadata @@ -17,7 +19,11 @@ module ActiveStorage def metadata read_image do |image| - { width: image.width, height: image.height } + if rotated_image?(image) + { width: image.height, height: image.width } + else + { width: image.width, height: image.height } + end end rescue LoadError logger.info "Skipping image analysis because the mini_magick gem isn't installed" @@ -31,5 +37,9 @@ module ActiveStorage yield MiniMagick::Image.new(file.path) end end + + def rotated_image?(image) + %w[ RightTop LeftBottom ].include?(image["orientation"]) + end end end diff --git a/activestorage/test/analyzer/image_analyzer_test.rb b/activestorage/test/analyzer/image_analyzer_test.rb index 0d9f24c5c1..f04ed63c3c 100644 --- a/activestorage/test/analyzer/image_analyzer_test.rb +++ b/activestorage/test/analyzer/image_analyzer_test.rb @@ -8,7 +8,15 @@ require "active_storage/analyzer/image_analyzer" class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase test "analyzing a JPEG image" do blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg") - metadata = blob.tap(&:analyze).metadata + metadata = extract_metadata_from(blob) + + assert_equal 4104, metadata[:width] + assert_equal 2736, metadata[:height] + end + + test "analyzing a rotated JPEG image" do + blob = create_file_blob(filename: "racecar_rotated.jpg", content_type: "image/jpeg") + metadata = extract_metadata_from(blob) assert_equal 4104, metadata[:width] assert_equal 2736, metadata[:height] @@ -16,9 +24,14 @@ class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase test "analyzing an SVG image without an XML declaration" do blob = create_file_blob(filename: "icon.svg", content_type: "image/svg+xml") - metadata = blob.tap(&:analyze).metadata + metadata = extract_metadata_from(blob) assert_equal 792, metadata[:width] assert_equal 584, metadata[:height] end + + private + def extract_metadata_from(blob) + blob.tap(&:analyze).metadata + end end diff --git a/activestorage/test/fixtures/files/racecar_rotated.jpg b/activestorage/test/fixtures/files/racecar_rotated.jpg new file mode 100644 index 0000000000..89e6d54f98 Binary files /dev/null and b/activestorage/test/fixtures/files/racecar_rotated.jpg differ -- cgit v1.2.3