62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/asciidoctor-diagram/meme/converter.rb', line 62
def convert(source, format, options)
magick = source.find_command('magick', :raise_on_error => false)
if magick
convert = ->(*args) { Cli.run(magick, 'convert', *args) }
identify = ->(*args) { Cli.run(magick, 'identify', *args) }
else
convert_cmd = source.find_command('convert')
convert = ->(*args) { Cli.run(convert_cmd, *args) }
identify_cmd = source.find_command('identify')
identify = ->(*args) { Cli.run(identify_cmd, *args) }
end
bg_img = options[:bg_img]
raise "background attribute is required" unless bg_img
bg_img = source.resolve_path(bg_img, options[:imagesdir])
top_label = options[:top_label]
bottom_label = options[:bottom_label]
fill_color = options[:fill_color] || 'white'
stroke_color = options[:stroke_color] || 'black'
stroke_width = options[:stroke_width] || '2'
font = options[:font] || 'Impact'
noupcase = options[:noupcase]
dimensions = identify.call('-format', '%w %h', bg_img)[:out].match(/(?<w>\d+) (?<h>\d+)/)
bg_width = dimensions['w'].to_i
bg_height = dimensions['h'].to_i
label_width = bg_width
label_height = bg_height / 5
if top_label
top_img = Tempfile.new(['meme', '.png'])
convert.call(
'-background', 'none',
'-fill', fill_color,
'-stroke', stroke_color,
'-strokewidth', stroke_width,
'-font', font,
'-size', "#{label_width}x#{label_height}",
'-gravity', 'north',
"label:#{prepare_label(top_label, noupcase)}",
top_img.path
)
else
top_img = nil
end
if bottom_label
bottom_img = Tempfile.new(['meme', '.png'])
convert.call(
'-background', 'none',
'-fill', fill_color,
'-stroke', stroke_color,
'-strokewidth', stroke_width,
'-font', font,
'-size', "#{label_width}x#{label_height}",
'-gravity', 'south',
"label:#{prepare_label(bottom_label, noupcase)}",
bottom_img.path
)
else
bottom_img = nil
end
final_img = Tempfile.new(['meme', ".#{format.to_s}"])
args = [bg_img]
if top_img
args << top_img.path << '-geometry' << '+0+0' << '-composite'
end
if bottom_img
args << bottom_img.path << '-geometry' << "+0+#{bg_height - label_height}" << '-composite'
end
args << final_img.path
convert.call(*args)
File.binread(final_img)
end
|