123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Analyzer::ImageAnalyzer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveStorage::Analyzer
Defined in: activestorage/lib/active_storage/analyzer/image_analyzer.rb

Overview

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).
# => { width: 4104, height: 2736 }

This analyzer relies on the third-party MiniMagick gem. MiniMagick requires the ImageMagick system library.

Class Method Summary

::ActiveStorage::Analyzer - Inherited

.accept?

Implement this method in a concrete subclass.

.new

Instance Attribute Summary

Instance Method Summary

::ActiveStorage::Analyzer - Inherited

#metadata

Override this method in a concrete subclass.

#logger

::ActiveStorage::Downloading - Included

#download_blob_to

Efficiently downloads blob data into the given file.

#download_blob_to_tempfile

Opens a new tempfile in #tempdir and copies blob data into it.

#tempdir

Returns the directory in which tempfiles should be opened.

Constructor Details

This class inherits a constructor from ActiveStorage::Analyzer

Class Method Details

.accept?(blob) ⇒ Boolean

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/image_analyzer.rb', line 16

def self.accept?(blob)
  blob.image?
end

Instance Method Details

#metadata

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/image_analyzer.rb', line 20

def 
  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