class ActiveStorage::Analyzer::ImageAnalyzer

Parent:
ActiveStorage::Analyzer

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) Show source
# File activestorage/lib/active_storage/analyzer/image_analyzer.rb, line 16
def self.accept?(blob)
  blob.image?
end

Public Instance Methods

metadata() Show source
# File activestorage/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
end

© 2004–2019 David Heinemeier Hansson
Licensed under the MIT License.