forked from hanguofeng/gocaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcimage.go
164 lines (143 loc) · 3.09 KB
/
cimage.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
// Copyright 2013 hanguofeng. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gocaptcha
import (
"image"
"image/color"
"image/draw"
"github.com/hanguofeng/freetype-go-mirror/freetype"
)
const (
colorCount = 20
)
//CImage is a image process tool
type CImage struct {
*image.Paletted
config *ImageConfig
}
func (m *CImage) drawHorizLine(fromX, toX, y int, colorIdx uint8) *CImage {
if 0 >= colorIdx || colorIdx > colorCount {
colorIdx = uint8(rnd(0, colorCount))
}
for x := fromX; x <= toX; x++ {
m.SetColorIndex(x, y, colorIdx)
}
return m
}
func (m *CImage) drawCircle(x, y, radius int, colorIdx uint8) {
f := 1 - radius
dfx := 1
dfy := -2 * radius
xo := 0
yo := radius
m.SetColorIndex(x, y+radius, colorIdx)
m.SetColorIndex(x, y-radius, colorIdx)
m.drawHorizLine(x-radius, x+radius, y, colorIdx)
for xo < yo {
if f >= 0 {
yo--
dfy += 2
f += dfy
}
xo++
dfx += 2
f += dfx
m.drawHorizLine(x-xo, x+xo, y+yo, colorIdx)
m.drawHorizLine(x-xo, x+xo, y-yo, colorIdx)
m.drawHorizLine(x-yo, x+yo, y+xo, colorIdx)
m.drawHorizLine(x-yo, x+yo, y-xo, colorIdx)
}
}
func (m *CImage) drawString(text string) *CImage {
fg, bg := image.Black, &image.Uniform{color.RGBA{255, 255, 255, 255}}
draw.Draw(m, m.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetFontSize(m.config.FontSize)
c.SetClip(m.Bounds())
c.SetDst(m)
c.SetSrc(fg)
i := 0
fontsize := int(c.PointToFix32(m.config.FontSize) >> 8)
for _, s := range text {
c.SetFont(m.config.fontManager.GetRandomFont())
charX := (fontsize) * i + int((m.config.Width - 4*fontsize)/2)
charY := fontsize
charPt := freetype.Pt(charX, charY)
c.DrawString(string(s), charPt)
i = i + 1
}
return m
}
//CreateCImage will create a CImage struct with config
func CreateCImage(config *ImageConfig) *CImage {
r := new(CImage)
r.Paletted = image.NewPaletted(image.Rect(0, 0, config.Width, config.Height), randomPalette())
r.config = config
if nil == r.config.fontManager {
fm := CreateFontManager()
for _, fontFile := range config.FontFiles {
fm.AddFont(fontFile)
}
r.config.fontManager = fm
}
return r
}
func randomPalette() color.Palette {
p := make([]color.Color, colorCount+1)
// Transparent color.
p[0] = color.RGBA{0xFF, 0xFF, 0xFF, 0x00}
// Primary color.
prim := color.RGBA{
uint8(247),
uint8(247),
uint8(247),
0xFF,
}
content := color.RGBA{
uint8(37),
uint8(204),
uint8(167),
0xFF,
}
p[1] = prim
// Circle colors.
for i := 2; i <= colorCount; i++ {
p[i] = randomBrightness(content, 255)
}
return p
}
func randomBrightness(c color.RGBA, max uint8) color.RGBA {
minc := min3(c.R, c.G, c.B)
maxc := max3(c.R, c.G, c.B)
if maxc > max {
return c
}
n := rnd(0, int(max-maxc)) - int(minc)
return color.RGBA{
uint8(int(c.R) + n),
uint8(int(c.G) + n),
uint8(int(c.B) + n),
uint8(c.A),
}
}
func min3(x, y, z uint8) (m uint8) {
m = x
if y < m {
m = y
}
if z < m {
m = z
}
return
}
func max3(x, y, z uint8) (m uint8) {
m = x
if y > m {
m = y
}
if z > m {
m = z
}
return
}