Class: DragonflyLibvips::Processors::Encode

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_libvips/processors/encode.rb

Constant Summary collapse

FORMATS_WITHOUT_PROFILE_SUPPORT =
%w[dz webp hdr]

Instance Method Summary collapse

Instance Method Details

#call(content, format, options = {}) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dragonfly_libvips/processors/encode.rb', line 6

def call(content, format, options = {})
  raise UnsupportedFormat unless content.ext
  raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext.downcase)

  format = format.to_s
  format = 'tif' if format == 'tiff'
  format = 'jpg' if format == 'jpeg'

  raise UnsupportedOutputFormat unless SUPPORTED_OUTPUT_FORMATS.include?(format.downcase)

  if content.mime_type == Rack::Mime.mime_type(".#{format}")
    content.ext ||= format
    content.meta['format'] = format
    return
  end

  options = options.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v } # stringify keys

  input_options = options.fetch('input_options', {})
  output_options = options.fetch('output_options', {})

  input_options['access'] ||= 'sequential'
  if content.mime_type == 'image/jpeg'
    input_options['autorotate'] = true unless input_options.has_key?('autorotate')
  end

  output_options['profile'] ||= EPROFILE_PATH
  output_options.delete('profile') if FORMATS_WITHOUT_PROFILE_SUPPORT.include?(format)

  require 'vips'
  img = ::Vips::Image.new_from_file(content.path, input_options)

  content.update(
    img.write_to_buffer(".#{format}", output_options),
    'name' => "temp.#{format}",
    'format' => format
  )
  content.ext = format
end

#update_url(url_attributes, format, options = {}) ⇒ Object



46
47
48
# File 'lib/dragonfly_libvips/processors/encode.rb', line 46

def update_url(url_attributes, format, options = {})
  url_attributes.ext = format.to_s
end