class Shrine::Plugins::StoreDimensions::DimensionsAnalyzer

Constants

SUPPORTED_TOOLS

Public Class Methods

new(tool, on_error: method(:fail)) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 101
def initialize(tool, on_error: method(:fail))
  raise Error, "unknown dimensions analyzer #{tool.inspect}, supported analyzers are: #{SUPPORTED_TOOLS.join(",")}" unless SUPPORTED_TOOLS.include?(tool)

  @tool     = tool
  @on_error = on_error
end

Public Instance Methods

call(io) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 108
def call(io)
  dimensions = send(:"extract_with_#{@tool}", io)
  io.rewind
  dimensions
end

Private Instance Methods

extract_with_fastimage(io) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 116
def extract_with_fastimage(io)
  require "fastimage"

  begin
    FastImage.size(io, raise_on_failure: true)
  rescue FastImage::FastImageException => error
    on_error(error)
  end
end
extract_with_mini_magick(io) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 126
def extract_with_mini_magick(io)
  require "mini_magick"

  begin
    Shrine.with_file(io) { |file| MiniMagick::Image.new(file.path).dimensions }
  rescue MiniMagick::Error => error
    on_error(error)
  end
end
extract_with_ruby_vips(io) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 136
def extract_with_ruby_vips(io)
  require "vips"

  begin
    Shrine.with_file(io) { |file| Vips::Image.new_from_file(file.path).size }
  rescue Vips::Error => error
    on_error(error)
  end
end
on_error(error) click to toggle source
# File lib/shrine/plugins/store_dimensions.rb, line 146
def on_error(error)
  @on_error.call(error)
  nil
end