Sha256: 912e0bdaa10630712e9a4037729677f17879cb85c84e178635c99cd0e9630ddd

Contents?: true

Size: 1.28 KB

Versions: 32

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

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
  #   # => { width: 4104, height: 2736 }
  #
  # This analyzer relies on the third-party {MiniMagick}[https://github.com/minimagick/minimagick] gem. MiniMagick requires
  # the {ImageMagick}[http://www.imagemagick.org] system library.
  class Analyzer::ImageAnalyzer < Analyzer
    def self.accept?(blob)
      blob.image?
    end

    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
      def read_image
        download_blob_to_tempfile do |file|
          require "mini_magick"
          yield MiniMagick::Image.new(file.path)
        end
      end

      def rotated_image?(image)
        %w[ RightTop LeftBottom ].include?(image["%[orientation]"])
      end
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
activestorage-5.2.3.rc1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.0.0.beta3 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.2.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.0.0.beta2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.0.0.beta1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.2.rc1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.1.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.1.rc1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.0 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.0.rc2 lib/active_storage/analyzer/image_analyzer.rb