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/
DPI =
300

Instance Method Summary collapse

Instance Method Details

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

Raises:



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dragonfly_libvips/processors/thumb.rb', line 11

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

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

  filename = content.path
  format = options.fetch('format', content.ext).to_s

  input_options = options.fetch('input_options', {})
  input_options['access'] = input_options.fetch('access', 'sequential')
  input_options['autorotate'] = input_options.fetch('autorotate', true) if content.mime_type == 'image/jpeg'

  if content.mime_type == 'application/pdf'
    input_options['dpi'] = input_options.fetch('dpi', DPI)
    input_options['page'] = input_options.fetch('page', 0)
  else
    input_options.delete('page')
    input_options.delete('dpi')
  end

  output_options = options.fetch('output_options', {})
  output_options['profile'] = input_options.fetch('profile', EPROFILE_PATH)

  input_options = input_options.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v } # symbolize
  img = ::Vips::Image.new_from_file(filename, input_options)

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

  thumbnail_options = options.fetch('thumbnail_options', {})
  thumbnail_options['auto_rotate'] = input_options.fetch('autorotate', true) if content.mime_type == 'image/jpeg'
  thumbnail_options['height'] = thumbnail_options.fetch('height', dimensions.height.ceil)
  thumbnail_options['import_profile'] = CMYK_PROFILE_PATH if img.get('interpretation') == :cmyk
  thumbnail_options['size'] ||= case geometry
                               when />\z/ then :down # do_not_resize_if_image_smaller_than_requested
                               when /<\z/ then :up # do_not_resize_if_image_larger_than_requested
                               else :both
  end

  filename += "[page=#{input_options[:page]}]" if content.mime_type == 'application/pdf'

  thumbnail_options = thumbnail_options.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v } # symbolize
  thumb = ::Vips::Image.thumbnail(filename, dimensions.width.ceil, thumbnail_options)

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

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



66
67
68
69
70
# File 'lib/dragonfly_libvips/processors/thumb.rb', line 66

def update_url(url_attributes, _, options = {})
  options = options.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v } # stringify keys
  return unless format = options.fetch('format', nil)
  url_attributes.ext = format
end