Module: CarrierWave::WebP::Converter

Defined in:
lib/carrierwave/webp/converter.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_webp(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/carrierwave/webp/converter.rb', line 8

def convert_to_webp(options = {})
  manipulate! do |img|
    img          = yield(img) if block_given?
    webp_path    = "#{img.path}.webp"
    old_filename = filename

    ::WebP.encode(img.path, webp_path, options)

    # XXX: Hacks ahead!
    # I can't find any other way to store an alomost exact copy
    # of file for any particular version
    instance_variable_set('@filename', "#{filename}.webp")

    storage.store! SanitizedFile.new({
      tempfile: webp_path, filename: webp_path,
      content_type: 'image/webp'
    })

    instance_variable_set('@filename', old_filename)

    img
  end
end