Sha256: f873f77188e8829d4d229506db1e68ecfeb03d06f8835e2d8d50bc001d7ab2c5

Contents?: true

Size: 1.62 KB

Versions: 37

Compression:

Stored size: 1.62 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
    end

    private
      def read_image
        download_blob_to_tempfile do |file|
          require "mini_magick"
          image = MiniMagick::Image.new(file.path)

          if image.valid?
            yield image
          else
            logger.info "Skipping image analysis because ImageMagick doesn't support the file"
            {}
          end
        end
      rescue LoadError
        logger.info "Skipping image analysis because the mini_magick gem isn't installed"
        {}
      rescue MiniMagick::Error => error
        logger.error "Skipping image analysis due to an ImageMagick error: #{error.message}"
        {}
      end

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

Version data entries

37 entries across 37 versions & 4 rubygems

Version Path
activestorage-6.1.4.4 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.4.3 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.4.2 lib/active_storage/analyzer/image_analyzer.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/lib/active_storage/analyzer/image_analyzer.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.4.1 lib/active_storage/analyzer/image_analyzer.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activestorage-6.1.4/lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.4 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.3.2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.3.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.3 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.2.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.1 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.0 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.0.rc2 lib/active_storage/analyzer/image_analyzer.rb
activestorage-6.1.0.rc1 lib/active_storage/analyzer/image_analyzer.rb