-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (123 loc) · 3.58 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
package main
import (
"bytes"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"net/http"
)
type Profile struct {
Username string `json:"username"`
Avatar string `json:"avatar"`
Image string `json:"image"`
}
func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", fs)
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
var profiles = []Profile{
{
Username: "reposter",
Image: "https://imgur.com/fQDQJeg.jpg",
Avatar: "https://i.imgur.com/YgJ4qks.jpg",
},
}
for _, profile := range profiles {
img := Run(profile.Username, profile.Image, profile.Avatar)
buf := new(bytes.Buffer)
err := png.Encode(buf, img)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "image/jpeg")
w.Write(buf.Bytes())
return
}
}
// dimensions
const AvatarSize = 48
const BoxBorder = 10
const AvatarPositionX = 10
const UsernamePositionX = AvatarPositionX + AvatarSize + 5
const FontPoints = 35
func BoxHeight(h float64) float64 {
return float64((h * 6) / 100)
}
func CenterAvatar(imageHeight int) float64 {
return (float64(imageHeight) - AvatarSize) - ((BoxHeight(float64(imageHeight)) - AvatarSize) / 2)
}
func BoxWidth(dc *gg.Context, username string) float64 {
fontWidth, _ := dc.MeasureString(username)
return fontWidth + AvatarPositionX + UsernamePositionX + 10
}
func CenterUserNameY(imageHeight int, fontSize float64) float64 {
return CenterAvatar(imageHeight) + (fontSize / 2)
}
func Run(username, image, avatar string) image.Image {
// img
img := getImage(image)
g := img.Bounds()
imageWidth := g.Dx()
imageHeight := g.Dy()
dc := gg.NewContext(imageWidth, imageHeight)
_, fontHeight := dc.MeasureString(username)
loadFont(dc)
dc.DrawImage(img, 0, 0)
drawBox(dc, float64(imageHeight), username)
drawAvatar(dc, avatar, AvatarPositionX, CenterAvatar(imageHeight))
drawUsername(dc, username, float64(UsernamePositionX), CenterUserNameY(imageHeight, fontHeight))
return dc.Image()
}
func getImage(imageURL string) image.Image {
// get image
resp, _ := http.Get(imageURL)
defer resp.Body.Close()
// decode img
img, _ := jpeg.Decode(resp.Body)
return img
}
func drawBox(dc *gg.Context, imageHeight float64, username string) {
boxHeight := BoxHeight(imageHeight)
boxWidth := BoxWidth(dc, username)
borderRightSpacing := 10.0
//TODO: 350 magic number
dc.SetRGB255(255, 255, 255)
x := 0.0
y := imageHeight - boxHeight
dc.DrawRectangle(x, y, float64(boxWidth-borderRightSpacing), float64(boxHeight))
dc.FillPreserve()
dc.DrawRoundedRectangle(x, y, float64(boxWidth), float64(boxHeight), BoxBorder)
dc.FillPreserve()
dc.Clip()
}
func drawAvatar(dc *gg.Context, avatarURL string, x, y float64) {
// get avatar
avatarResp, _ := http.Get(avatarURL)
defer avatarResp.Body.Close()
avatarBody, _ := ioutil.ReadAll(avatarResp.Body)
// decode avatar
avatar, _, _ := image.Decode(bytes.NewReader(avatarBody))
dstImageFill := imaging.Fill(avatar, AvatarSize, AvatarSize, imaging.Center, imaging.Lanczos)
g := dstImageFill.Bounds()
avatarWidth := g.Dx()
avatarHeight := g.Dy()
dc.DrawRoundedRectangle(x, y, float64(avatarWidth), float64(avatarHeight), float64(avatarHeight)/2)
dc.Clip()
dc.DrawImage(dstImageFill, int(x), int(y))
dc.ResetClip()
}
func drawUsername(dc *gg.Context, username string, x, y float64) {
dc.SetRGB(0, 0, 0)
dc.DrawStringAnchored(username, x, y, 0, 1)
}
func loadFont(dc *gg.Context) {
if err := dc.LoadFontFace("./static_Oswald.ttf", FontPoints); err != nil {
panic(err)
}
}