-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
73 lines (62 loc) · 1.76 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra'
require 'RMagick'
require 'open-uri'
require 'json'
include Magick
get '/' do
erb :index
end
def max_pt_per_px(width_px, string)
str_length = string.split(//u).size
pt = (width_px / str_length).to_i
pt *= 2 if string =~ /\A[\(\)@\w\s]+\z/
return pt
end
get "/:user" do
begin
user_name = File.basename(params[:user])
unless File.exist?("/tmp/#{user_name}.jpg")
width = 8.9 / 2.54 * 72
height = 9.8 / 2.54 * 72
canvas = ImageList.new
canvas.new_image(width, height)
canvas.border!(2, 2, "black")
draw = Draw.new do
self.fill = 'black'
self.stroke = 'transparent'
self.font = File.expand_path('./fonts/ipag.ttf')
end
# Name
json = open("http://api.twitter.com/1/users/show/#{user_name}.json")
user = JSON.parse(json.read)
user_real_name = user["name"]
start_margin = 3
user_real_name.split(/\s/).each do |str|
pt = max_pt_per_px(width - 20, str)
draw.annotate(canvas, 0, 0, 10, start_margin, str) {
self.pointsize = pt
self.gravity = NorthWestGravity
}
start_margin += (pt - 5)
end
# Twitter id
draw.annotate(canvas, 0, 0, 10, 5, "@#{user_name}") {
self.pointsize = max_pt_per_px(width - 20 - 72, "@#{user_name}")
self.gravity = SouthWestGravity
}
# Twitter Image
icon = ImageList.new("http://img.tweetimag.es/i/#{user_name}_b")
canvas.composite!(icon, SouthEastGravity, 10, 10, OverCompositeOp)
canvas.write("/tmp/#{user_name}.jpg")
end
# read
File.open("/tmp/#{user_name}.jpg") do |f|
content_type :jpg
f.read
end
rescue
raise Sinatra::NotFound
end
end