-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.rb
58 lines (45 loc) · 1.71 KB
/
web.rb
1
2
3
4
5
6
7
8
9
10
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
require 'sinatra'
require 'RMagick'
require 'rvg/rvg'
require 'redcarpet'
get '/' do
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
markdown.render(File.read('./README.md'))
end
# matches up-to /{0:size}/{2:background}/{3:foreground}.{4:format}
get /([^\/]+)(\/([^\/]+)\/([^\/]+))?\.(\w+)/ do |size, _, background, foreground, format|
cache_control :public, :max_age => 36000
content_type "image/#{format}"
opts = { :full_mode => params[:mode] == 'full' }
to_image(size, background, foreground, format, params[:text], opts)
end
# matches up-to /{0:size}/{2:background}/{3:foreground}
get /([^\/]+)(\/([^\/]+)\/([^\/]+))?/ do |size, _, background, foreground|
cache_control :public, :max_age => 36000
content_type "image/png"
opts = { :full_mode => params[:mode] == 'full' }
to_image(size, background, foreground, 'png', params[:text], opts)
end
def to_image(size, background, foreground, format = 'png', text = nil, opts = {})
# defaults
background = background ? "##{background}" : 'black'
foreground = foreground ? "##{foreground}" : 'grey69'
if (text && !opts[:full_mode]) then
text = (text.split(/\s/)- ["", nil]).map { |t| t[0].upcase }.slice(0, 3).join('')
end
w, h = size.split('x').map { |d| d.to_i }
h ||= w
text ||= "#{w}x#{h}"
rvg = Magick::RVG.new(w, h).viewbox(0, 0, w, h) do |canvas|
canvas.background_fill = background
end
img = rvg.draw
img.format = format
drawable = Magick::Draw.new
drawable.pointsize = w / text.length
drawable.font = ("./DroidSans.ttf")
drawable.fill = foreground
drawable.gravity = Magick::CenterGravity
drawable.annotate(img, 0, 0, 0, 0, text)
return img.to_blob
end