class ActiveStorage::Analyzer::ImageAnalyzer
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 # => { width: 4104, height: 2736 }
This analyzer relies on the third-party MiniMagick gem. MiniMagick requires the ImageMagick system library.
Public Class Methods
accept?(blob)
click to toggle source
# File lib/active_storage/analyzer/image_analyzer.rb, line 16 def self.accept?(blob) blob.image? end
Public Instance Methods
metadata()
click to toggle source
# File lib/active_storage/analyzer/image_analyzer.rb, line 20 def metadata read_image do |image| 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" {} end
Private Instance Methods
read_image() { |image| ... }
click to toggle source
# File lib/active_storage/analyzer/image_analyzer.rb, line 34 def read_image download_blob_to_tempfile do |file| require "mini_magick" yield MiniMagick::Image.new(file.path) end end
rotated_image?(image)
click to toggle source
# File lib/active_storage/analyzer/image_analyzer.rb, line 41 def rotated_image?(image) %w[ RightTop LeftBottom ].include?(image["%[orientation]"]) end