Module: Shrine::Plugins::Blurhash::ClassMethods

Defined in:
lib/shrine/plugins/blurhash.rb

Instance Method Summary collapse

Instance Method Details

#compute_blurhash(io) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shrine/plugins/blurhash.rb', line 39

def compute_blurhash(io)
  extractor = opts[:blurhash][:extractor]
  extractor = pixels_extractor(extractor) if extractor.is_a?(Symbol)

  resize_to = opts[:blurhash][:resize_to]
  args = [io, resize_to, pixels_extractors].take(extractor.arity.abs)

  blurhash = instrument_blurhash(io) do
    pixels = extractor.call(*args)

    x_comp, y_comp = components_for(pixels[:width], pixels[:height])
    ::Blurhash.encode(pixels[:width], pixels[:height], pixels[:pixels], x_comp: x_comp, y_comp: y_comp)
  end

  io.rewind

  blurhash
end

#pixels_extractor(name) ⇒ Object

Returns callable pixels extractor object.



68
69
70
71
72
# File 'lib/shrine/plugins/blurhash.rb', line 68

def pixels_extractor(name)
  on_error = opts[:blurhash][:on_error]

  PixelsExtractor.new(name, on_error: on_error).method(:call)
end

#pixels_extractorsObject

Returns a hash of built-in pixels extractors, where keys are extractors names and values are ‘#call`-able objects which accepts the IO object.



61
62
63
64
65
# File 'lib/shrine/plugins/blurhash.rb', line 61

def pixels_extractors
  @pixels_extractors ||= PixelsExtractor::SUPPORTED_EXTRACTORS.inject({}) do |hash, tool|
    hash.merge!(tool => pixels_extractor(tool))
  end
end