Sha256: 8e104e03ce0ac8cff51c0b0f57ea0e36efa5ce6bae20a00009a01e4290d794e5

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module ActiveStorage
  # Extracts width and height in pixels from an image blob.
  #
  # 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. These libraries are not provided by Rails; you must
  # install them yourself to use this analyzer.
  class Analyzer::ImageAnalyzer < Analyzer
    def self.accept?(blob)
      blob.image?
    end

    def metadata
      read_image do |image|
        { width: image.width, height: image.height }
      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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activestorage-5.2.0.beta2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-5.2.0.beta1 lib/active_storage/analyzer/image_analyzer.rb