-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (123 loc) · 3.18 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
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
func parseHexColor(s string, fallback color.RGBA) color.RGBA {
c := color.RGBA{}
c.A = 0xff
hexToByte := func(b byte) byte {
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
case b >= 'A' && b <= 'F':
return b - 'A' + 10
}
return 0
}
switch len(s) {
case 6:
c.R = hexToByte(s[0])<<4 + hexToByte(s[1])
c.G = hexToByte(s[2])<<4 + hexToByte(s[3])
c.B = hexToByte(s[4])<<4 + hexToByte(s[5])
case 3:
c.R = hexToByte(s[0]) * 17
c.G = hexToByte(s[1]) * 17
c.B = hexToByte(s[2]) * 17
default:
return fallback
}
return c
}
func addLabel(img *image.RGBA, width int, height int, label string, size int, fg color.RGBA) {
// Read the font data.
fontBytes, err := ioutil.ReadFile("Inconsolata.ttf")
if err != nil {
log.Println(err)
return
}
f, err := truetype.Parse(fontBytes)
if err != nil {
log.Println(err)
return
}
// Draw the text.
h := font.HintingFull
dpi := 72.0
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(fg),
Face: truetype.NewFace(f, &truetype.Options{
Size: float64(size),
DPI: dpi,
Hinting: h,
}),
}
d.Dot = fixed.Point26_6{
X: (fixed.I(width) - d.MeasureString(label)) / 2,
Y: (fixed.I(height) + fixed.I(int(float64(size)*0.6))) / 2,
}
d.DrawString(label)
}
func createImage(width int, height int, bgColor color.RGBA) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, bgColor)
}
}
return img
}
func validatePixelDimension(value int) int {
if value <= 0 || 4000 < value {
value = 200
}
return value
}
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
headers := map[string]string{
"Content-Type": "image/png",
"Expires": time.Now().AddDate(1, 0, 0).Format(http.TimeFormat),
"Last-Modified": time.Now().Format(http.TimeFormat),
"Access-Control-Allow-Origin": "*",
}
params := request.QueryStringParameters
x, _ := strconv.Atoi(params["x"])
x = validatePixelDimension(x)
y, _ := strconv.Atoi(params["y"])
y = validatePixelDimension(y)
fontSize, _ := strconv.Atoi(params["fontSize"])
label, _ := params["label"]
if label == "" || len(label) > 20 {
label = fmt.Sprintf("%d x %d", x, y)
}
bgColor := parseHexColor(params["bg"], color.RGBA{255, 255, 255, 255})
fgColor := parseHexColor(params["fg"], color.RGBA{51, 51, 51, 255})
image := createImage(x, y, bgColor)
addLabel(image, x, y, label, fontSize, fgColor)
buf := new(bytes.Buffer)
png.Encode(buf, image)
b64Image := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println(b64Image)
return events.APIGatewayProxyResponse{StatusCode: 200, Headers: headers, Body: b64Image, IsBase64Encoded: true}, nil
}
func main() {
lambda.Start(Handler)
}