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 }
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 } 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 when /<\z/ then :up 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 } 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
|