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
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/dragonfly_libvips/processors/thumb.rb', line 13
def call(content, geometry, options = {})
raise UnsupportedFormat unless content.ext
raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext.downcase)
options = DragonflyLibvips.stringify_keys(options)
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", {})
if FORMATS_WITHOUT_PROFILE_SUPPORT.include?(format)
output_options.delete("profile")
else
output_options["profile"] ||= input_options.fetch("profile", EPROFILE_PATH)
end
output_options.delete("Q") unless /jpg|jpeg/i.match?(format.to_s)
output_options["format"] ||= format.to_s if /bmp/i.match?(format.to_s)
input_options = input_options.transform_keys { |k| k.to_sym } img = ::Vips::Image.new_from_file(filename, **DragonflyLibvips.symbolize_keys(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", {})
if Vips.at_least_libvips?(8, 8)
thumbnail_options["no_rotate"] = input_options.fetch("no_rotate", false) if content.mime_type == "image/jpeg"
else
thumbnail_options["auto_rotate"] = input_options.fetch("autorotate", true) if content.mime_type == "image/jpeg"
end
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.transform_keys { |k| k.to_sym } thumb = ::Vips::Image.thumbnail(filename, dimensions.width.ceil, **DragonflyLibvips.symbolize_keys(thumbnail_options))
content.update(
thumb.write_to_buffer(".#{format}", **DragonflyLibvips.symbolize_keys(output_options)),
"name" => "temp.#{format}",
"format" => format
)
content.ext = format
end
|