-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
77 lines (69 loc) · 1.86 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
require 'sinatra'
require 'sinatra/reloader'
require 'slim'
require 'RMagick'
require 'date'
get '/' do
@title = 'lgtm_maker'
slim :index
end
post '/upload' do
if params[:photo]
datetime = DateTime.now.strftime('%Y-%m-%d-%H-%M-%s').to_s
image_path = "./public/images/#{datetime}_#{params[:photo][:filename]}"
File.open(image_path, 'wb') do |f|
p params[:photo][:tempfile]
f.write params[:photo][:tempfile].read
end
enchar image_path, params[:stroke]
end
redirect 'images'
end
get '/images' do
@title = 'local images'
images_name = Dir.glob('./public/images/*')
@images_path = []
images_name.sort!
images_name.each do |image|
@images_path << image.gsub('public/', './')
end
@images_path.sort!.reverse!
slim :images
end
helpers do
def enchar(image_path, stroke)
image_file_name = File.basename(image_path)
img = Magick::ImageList.new(image_path)
lgtm = 'LGTM'
char = "\nLooks Good To Me."
font = './public/fonts/Mamelon.otf'
pointsize = 120
fill = 'white'
if img.columns > 1200
scale = 1200.quo(img.columns).to_f
img = img.resize(scale)
end
writePic(img, lgtm, font, fill, pointsize, stroke, 0)
writePic(img, char, font, fill, pointsize / 3, stroke, 60)
img.write("public/images/#{image_file_name}")
img.destroy!
end
def writePic(img, char, font, fill, pointsize, stroke, hight)
draw = Magick::Draw.new
draw.annotate(img, 0, 0, 0, hight, char) do
self.font = font
self.fill = fill
self.pointsize = pointsize
self.stroke = stroke
self.stroke_width = 4
self.gravity = Magick::CenterGravity
end
draw.annotate(img, 0, 0, 0, hight, char) do
self.font = font
self.fill = fill
self.pointsize = pointsize
self.stroke = 'transparent'
self.gravity = Magick::CenterGravity
end
end
end