class Shrine::Plugins::Blurhash::PixelsExtractor

Constants

SUPPORTED_EXTRACTORS

Public Class Methods

new(tool, on_error: method(:fail)) click to toggle source
# File lib/shrine/plugins/blurhash.rb, line 102
def initialize(tool, on_error: method(:fail))
  unless SUPPORTED_EXTRACTORS.include?(tool)
    raise Error, "unknown pixel extractor #{tool.inspect}, supported extractors are: #{SUPPORTED_EXTRACTORS.join(',')}"
  end

  @tool     = tool
  @on_error = on_error
end

Public Instance Methods

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

Private Instance Methods

extract_with_ruby_vips(io, resize_to) click to toggle source
# File lib/shrine/plugins/blurhash.rb, line 119
def extract_with_ruby_vips(io, resize_to)
  require "vips"

  begin
    Shrine.with_file(io) do |file|
      image = Vips::Image.new_from_file(file.path, access: :sequential)
      image = image.resize(resize_to.fdiv(image.width), vscale: resize_to.fdiv(image.height)) if resize_to

      {
        width: image.width,
        height: image.height,
        pixels: image.to_a.flatten,
      }
    end
  rescue Vips::Error => e
    on_error(e)
  end
end
on_error(error) click to toggle source
# File lib/shrine/plugins/blurhash.rb, line 138
def on_error(error)
  @on_error.call(error)
  nil
end