-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
105 lines (90 loc) · 2.83 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
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
require "json"
require "mini_magick"
require "jellyfish"
class Say
include Jellyfish
SERVER_NAME = Settings.dns_name
get "/bg-list" do
folder = request.params["folder"] || ""
if folder != ""
files = Dir["public/images/#{folder}/*.jpg"]
array = []
files.each do |f|
path = f.sub("public/", "")
name = f.split("/").last(2).join("/").split(".").first
array << {
:url => "#{SERVER_NAME}/#{path}",
:name => name
}
end
array.to_json
else
{
:code => "params is empty."
}.to_json
end
end
post "/to" do
bg = request.params["bg"] || ""
quotes = request.params["quotes"] || ""
next {:code => "bg and quotes can't be blank."}.to_json if (bg == "") || (quotes == "")
folder, filename = bg.split("/")
quote1, quote2, quote3 = quotes.split("\n")
[quote1, quote2, quote3].each do |q|
next if q.nil?
q.strip!
q.gsub!("'", "\\\\'")
end
if quote2.nil? && quote3.nil?
quote2 = quote1
quote1 = nil
end
image = MiniMagick::Image.open("public/images/#{folder}/#{filename}.jpg")
case folder
when "vday2015", "201511sofina",
"2016changedestiny", "2016YoungFuture", "2016lemuria",
"201607skl", "201607BenQ", "201610samsung", "2016goldenhourse",
"201701innisfree", "201701MOMA", "201705Audrey", "201710DateMeNow",
"201911goh-theapologybook"
position = 60
quote_function1 = "text 0,-#{position} '#{quote1}'"
quote_function2 = "text 0,0 '#{quote2}'"
quote_function3 = "text 0,#{position} '#{quote3}'"
image.combine_options do |c|
c.font "fonts/NotoSansCJKtc-Bold.otf"
c.pointsize "40"
c.gravity "Center"
c.draw quote_function1
c.fill "white"
c.draw quote_function2
c.fill "white"
c.draw quote_function3
c.fill "white"
end
when "demo"
position = 80
height = 30
quote_function1 = "translate 90,#{position + height * 0} rotate -17 text 0,0 '#{quote1}'"
quote_function2 = "translate 100,#{position + height * 1} rotate -17 text 0,0 '#{quote2}'"
quote_function3 = "translate 110,#{position + height * 2} rotate -17 text 0,0 '#{quote3}'"
image.combine_options do |c|
c.font "fonts/NotoSansCJKtc-Bold.otf"
c.pointsize "20"
c.draw quote_function1
c.fill "black"
c.draw quote_function2
c.fill "black"
c.draw quote_function3
c.fill "black"
end
end
timestamp = Time.now.to_i
rand = rand(100)
image_with_quote = "#{filename}-#{timestamp}-#{rand}.jpg"
image.write("public/quotes/#{image_with_quote}")
{
:image => "#{SERVER_NAME}/quotes/#{image_with_quote}",
:code => 'success'
}.to_json
end
end