-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummage.go
135 lines (112 loc) · 2.68 KB
/
dummage.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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"io"
"log"
"math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
var imageNamePattern *regexp.Regexp
func init() {
imageNamePattern = regexp.MustCompile(`(?i)^(\d+)x(\d+)(\-[0-9a-f]{6})?.(jpe?g|png)$`)
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
host := flag.String("host", "localhost", "listen on this host")
port := flag.Int("port", 8000, "start server on this port")
flag.Parse()
url := fmt.Sprintf("%s:%d", *host, *port)
log.Printf("Starting server on %s\n", url)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(url, nil))
}
func randomColor() color.Color {
const max = 2 << 8
return color.RGBA{
uint8(rand.Intn(max)),
uint8(rand.Intn(max)),
uint8(rand.Intn(max)),
0xFF,
}
}
func parseImageConfig(name string) (width, height int, background color.Color, format string, err error) {
match := imageNamePattern.FindStringSubmatch(name)
if len(match) != 5 {
err = fmt.Errorf("Fail to parse name: %v", name)
return
}
widthStr, heightStr, colorStr, format := match[1], match[2], match[3], strings.ToUpper(match[4])
width, err = strconv.Atoi(widthStr)
if err == nil {
height, err = strconv.Atoi(heightStr)
}
if err != nil {
return
}
if len(colorStr) > 0 {
background = parseColor(colorStr[1:])
} else {
background = randomColor()
}
return
}
func parseColor(s string) color.Color {
var (
err error
r, g, b uint64
)
r, err = strconv.ParseUint(s[0:2], 16, 8)
if err == nil {
g, err = strconv.ParseUint(s[2:4], 16, 8)
}
if err == nil {
b, err = strconv.ParseUint(s[4:6], 16, 8)
}
if err != nil {
log.Printf("Fail to parse color: %v, using default color\n%s", s, err)
return randomColor()
}
return color.RGBA{uint8(r), uint8(g), uint8(b), 0xFF}
}
func handler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimLeft(r.URL.String(), "/")
width, height, background, format, err := parseImageConfig(name)
if err != nil {
log.Println(err)
http.NotFound(w, r)
return
}
img := createImage(width, height, background)
if format == "JPG" || format == "JPEG" {
err = writeJPEG(w, img)
} else {
err = writePNG(w, img)
}
if err != nil {
log.Panic(err)
}
}
func writeJPEG(w io.Writer, img image.Image) error {
var opt jpeg.Options
opt.Quality = 80
return jpeg.Encode(w, img, &opt)
}
func writePNG(w io.Writer, img image.Image) error {
return png.Encode(w, img)
}
func createImage(width, height int, background color.Color) *image.RGBA {
rect := image.Rect(0, 0, width, height)
img := image.NewRGBA(rect)
draw.Draw(img, img.Bounds(), &image.Uniform{background}, image.ZP, draw.Src)
return img
}