Class: DragonflyLibvips::Processors::Thumb

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

Constant Summary collapse

OPERATORS =
'><'.freeze
RESIZE_GEOMETRY =

e.g. ‘300x200>’

/\A\d*x\d*[#{OPERATORS}]?\z/
RESIZE_KEYS =
%w(kernel).freeze

Instance Method Summary collapse

Instance Method Details

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



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
# File 'lib/dragonfly_libvips/processors/thumb.rb', line 11

def call(content, geometry, options = {})
  options = options.deep_stringify_keys

  format = options.fetch('format', content.ext)

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

  input_options['access'] ||= 'sequential'
  output_options['profile'] ||= DragonflyLibvips::EPROFILE_PATH

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

  dimensions = case geometry
               when RESIZE_GEOMETRY then DragonflyLibvips::Dimensions.call(geometry, img.width, img.height)
               else raise ArgumentError, "Didn't recognise the geometry string #{geometry}"
  end

  if dimensions.scale != 1
    img = img.resize(dimensions.scale, resize_options)
  end

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

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



39
40
41
42
43
44
45
# File 'lib/dragonfly_libvips/processors/thumb.rb', line 39

def update_url(url_attributes, _, options = {})
  options = options.deep_stringify_keys

  if format = options.fetch('format', nil)
    url_attributes.ext = format
  end
end