-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
187 lines (158 loc) · 5 KB
/
main.go
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bytes"
"embed"
"encoding/base64"
"flag"
"fmt"
"github.com/golang/freetype"
"github.com/gorilla/csrf"
"github.com/johnpili/go-text-to-image/models"
"github.com/johnpili/go-text-to-image/page"
"github.com/julienschmidt/httprouter"
"golang.org/x/image/font"
"image"
"image/color"
"image/draw"
"image/png"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var (
configuration models.Config
fontFile string
//go:embed static/*
static embed.FS
//go:embed views/*
views embed.FS
)
func main() {
pid := os.Getpid()
err := os.WriteFile("application.pid", []byte(strconv.Itoa(pid)), 0666)
if err != nil {
log.Fatal(err)
}
var configLocation string
flag.StringVar(&configLocation, "config", "config.yml", "Set the location of configuration file")
flag.Parse()
loadConfiguration(configLocation, &configuration)
f := fs.FS(static)
v, _ := fs.Sub(f, "static")
router := httprouter.New()
router.ServeFiles(fmt.Sprintf("%s%s", configuration.HTTP.BasePath, "/static/*filepath"), http.FS(v))
router.HandlerFunc("GET", fmt.Sprintf("%s%s", configuration.HTTP.BasePath, "/"), indexHandler)
router.HandlerFunc("POST", fmt.Sprintf("%s%s", configuration.HTTP.BasePath, "/"), indexHandler)
csrfProtection := csrf.Protect(generateRandomBytes(32))
port := strconv.Itoa(configuration.HTTP.Port)
httpServer := &http.Server{
Addr: ":" + port,
Handler: csrfProtection(router),
ReadTimeout: 120 * time.Second,
WriteTimeout: 120 * time.Second,
}
if configuration.HTTP.IsTLS {
log.Printf("Server running at https://localhost:%s%s/\n", port, configuration.HTTP.BasePath)
log.Fatal(httpServer.ListenAndServeTLS(configuration.HTTP.ServerCert, configuration.HTTP.ServerKey))
return
}
log.Printf("Server running at http://localhost:%s%s/\n", port, configuration.HTTP.BasePath)
log.Fatal(httpServer.ListenAndServe())
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
{
p := page.New()
p.Title = "Generate Text to Image in Go"
p.CSRFToken = csrf.Token(r)
renderPage(w, r, p, configuration.HTTP.BasePath, "views/base.html", "views/text-to-image-builder.html")
}
case http.MethodPost:
{
textContent := strings.Trim(r.FormValue("textContent"), " ")
fontSize, err := strconv.ParseFloat(strings.Trim(r.FormValue("fontSize"), " "), 64)
if err != nil {
fontSize = 32
}
fontName := strings.Trim(r.FormValue("fontName"), " ")
fontColorHex := strings.Trim(r.FormValue("fontColor"), " ")
fontColorHex = strings.ToLower(fontColorHex)
backgroundColorHex := strings.Trim(r.FormValue("backgroundColor"), " ")
backgroundColorHex = strings.ToLower(backgroundColorHex)
b, err := generateImage(textContent, fontColorHex, backgroundColorHex, fontName, fontSize)
if err != nil {
log.Println(err)
return
}
str := base64.StdEncoding.EncodeToString(b)
p := page.New()
p.Title = "Generate Text to Image in Go"
p.CSRFToken = csrf.Token(r)
data := make(map[string]interface{})
data["generatedImage"] = str
p.SetData(data)
renderPage(w, r, p, configuration.HTTP.BasePath, "views/base.html", "views/text-to-image-result.html")
}
default:
{
}
}
}
func generateImage(textContent string, fgColorHex string, bgColorHex string, fontName string, fontSize float64) ([]byte, error) {
fgColor := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} // Default font color
if len(fgColorHex) == 7 {
_, err := fmt.Sscanf(fgColorHex, "#%02x%02x%02x", &fgColor.R, &fgColor.G, &fgColor.B)
if err != nil {
log.Println(err)
fgColor = color.RGBA{R: 0x2e, G: 0x34, B: 0x36, A: 0xff}
}
}
bgColor := color.RGBA{R: 0x30, G: 0x0a, B: 0x24, A: 0xff} // Default background color
if len(bgColorHex) == 7 {
_, err := fmt.Sscanf(bgColorHex, "#%02x%02x%02x", &bgColor.R, &bgColor.G, &bgColor.B)
if err != nil {
log.Println(err)
bgColor = color.RGBA{R: 0x30, G: 0x0a, B: 0x24, A: 0xff}
}
}
loadedFont, err := loadFont(fontName)
if err != nil {
log.Println(err)
return nil, err
}
code := strings.Replace(textContent, "\t", " ", -1) // convert tabs into spaces
text := strings.Split(code, "\n") // split newlines into arrays
fg := image.NewUniform(fgColor)
bg := image.NewUniform(bgColor)
rgba := image.NewRGBA(image.Rect(0, 0, 1200, 630))
draw.Draw(rgba, rgba.Bounds(), bg, image.Pt(0, 0), draw.Src)
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(loadedFont)
c.SetFontSize(fontSize)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
c.SetHinting(font.HintingNone)
textXOffset := 50
textYOffset := 10 + int(c.PointToFixed(fontSize)>>6) // Note shift/truncate 6 bits first
pt := freetype.Pt(textXOffset, textYOffset)
for _, s := range text {
_, err = c.DrawString(strings.Replace(s, "\r", "", -1), pt)
if err != nil {
return nil, err
}
pt.Y += c.PointToFixed(fontSize * 1.5)
}
b := new(bytes.Buffer)
if err := png.Encode(b, rgba); err != nil {
log.Println("unable to encode image.")
return nil, err
}
return b.Bytes(), nil
}