aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2018-01-31 15:43:29 -0500
committerGeorge Claghorn <george@basecamp.com>2018-01-31 15:43:29 -0500
commitf9b806eaa18c7bdaedb36a073a450f5fa6417d2e (patch)
tree0959205cfbb24b132ff8ecf487fd91671edf023b /activestorage/lib
parent148d0077c539ebca99a8cf776902805da3431f95 (diff)
downloadrails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.tar.gz
rails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.tar.bz2
rails-f9b806eaa18c7bdaedb36a073a450f5fa6417d2e.zip
Swap encoded image width and height if angle is 90 or 270 degrees
Diffstat (limited to 'activestorage/lib')
-rw-r--r--activestorage/lib/active_storage/analyzer/image_analyzer.rb12
1 files changed, 11 insertions, 1 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