diff options
author | George Claghorn <george@basecamp.com> | 2018-01-31 15:43:29 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-01-31 15:43:29 -0500 |
commit | f9b806eaa18c7bdaedb36a073a450f5fa6417d2e (patch) | |
tree | 0959205cfbb24b132ff8ecf487fd91671edf023b | |
parent | 148d0077c539ebca99a8cf776902805da3431f95 (diff) | |
download | rails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.tar.gz rails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.tar.bz2 rails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.zip |
Swap encoded image width and height if angle is 90 or 270 degrees
-rw-r--r-- | activestorage/lib/active_storage/analyzer/image_analyzer.rb | 12 | ||||
-rw-r--r-- | activestorage/test/analyzer/image_analyzer_test.rb | 17 | ||||
-rw-r--r-- | activestorage/test/fixtures/files/racecar_rotated.jpg | bin | 0 -> 1124060 bytes |
3 files changed, 26 insertions, 3 deletions
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 Binary files differnew file mode 100644 index 0000000000..89e6d54f98 --- /dev/null +++ b/activestorage/test/fixtures/files/racecar_rotated.jpg |