-
Notifications
You must be signed in to change notification settings - Fork 2
/
avatar.go
217 lines (168 loc) · 4.53 KB
/
avatar.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package avatar
import (
"bytes"
"errors"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
"math"
"github.com/nfnt/resize"
)
type Avatar interface {
Source() []byte // get source content for avatar generation
Square() ([]byte, error) // generates the square avatar
Circle() ([]byte, error) // generates the round avatar
loadOriginalImage() (image.Image, error)
originalImg() image.Image
}
type AvatarOptions interface {
bgColor() color.Color
size() int
}
type dimensions struct {
w int
h int
}
var (
defaultSize = 300
defaultBgColor = color.RGBA{215, 0, 255, 255}
defaultTxtColor = color.White
defaultNInitials = 1
)
func NewAvatarFromInitials(text []byte, options *InitialsOptions) (*Initials, error) {
if options == nil {
options = &InitialsOptions{}
}
newAvatar := Initials{
source: text,
options: options,
}
original, err := newAvatar.loadOriginalImage()
if err != nil {
return nil, err
}
newAvatar.originalImage = original
return &newAvatar, nil
}
func NewAvatarFromPic(pic []byte, options *PictureOptions) (*Picture, error) {
if options == nil {
options = &PictureOptions{}
}
newAvatar := Picture{
source: pic,
options: options,
}
original, err := newAvatar.loadOriginalImage()
if err != nil {
return nil, err
}
newAvatar.originalImage = original
return &newAvatar, nil
}
func bgColor(bgColor color.Color) color.Color {
if bgColor == nil {
return defaultBgColor
}
return bgColor
}
func size(size int) int {
if size > 0 {
return size
}
return defaultSize
}
func square(a Avatar, options AvatarOptions) ([]byte, error) {
squareImg, err := generateSquareImage(a)
if err != nil {
return []byte{}, err
}
size := defaultSize
if options != nil {
size = options.size()
}
return encode(squareImg, size)
}
func circle(a Avatar, options AvatarOptions) ([]byte, error) {
circleImg, err := generateCircleImage(a, options.bgColor())
if err != nil {
return []byte{}, err
}
size := defaultSize
if options != nil {
size = options.size()
}
return encode(circleImg, size)
}
func encode(img image.Image, size int) ([]byte, error) {
var imgOutput []byte
buf := bytes.NewBuffer(imgOutput)
resized := resize.Resize(uint(size), uint(size), img, resize.Lanczos2)
err := png.Encode(buf, resized)
if err != nil {
return imgOutput, err
}
return buf.Bytes(), nil
}
// Computes the original image to generate the square image
// from the center of the original image
func generateSquareImage(a Avatar) (image.Image, error) {
if a.originalImg() == nil {
return nil, errors.New("Cannot create square image without original image")
}
oImage := a.originalImg()
minSize := getMinSide(oImage.Bounds())
dstRect := image.Rect(0, 0, minSize, minSize)
dstImg := image.NewRGBA(dstRect)
draw.Draw(dstImg, dstImg.Bounds(), &image.Uniform{color.Transparent}, image.ZP, draw.Src)
squareRect := getMaskCenterBounds(oImage.Bounds())
draw.DrawMask(dstImg, dstImg.Bounds(), oImage, image.Point{X: squareRect.Min.X, Y: squareRect.Min.Y}, dstRect, image.ZP, draw.Over)
return dstImg, nil
}
// Computes the square image to generate the round image
// from the center of the original image
func generateCircleImage(a Avatar, bgColor color.Color) (image.Image, error) {
sqImg, err := generateSquareImage(a)
if err != nil {
return nil, err
}
size := getMinSide(sqImg.Bounds())
dstRect := image.Rect(0, 0, size, size)
dstImg := image.NewRGBA(dstRect)
draw.Draw(dstImg, dstRect, &image.Uniform{color.Transparent}, image.ZP, draw.Src)
circleMask := &Circle{
p: image.Pt(int(float64(size)/2), int(float64(size)/2)),
r: size / 2,
}
draw.DrawMask(dstImg, sqImg.Bounds(), sqImg, image.ZP, circleMask, image.ZP, draw.Over)
return dstImg, nil
}
func getMaskCenterBounds(rect image.Rectangle) image.Rectangle {
srcBounds := rect.Bounds()
center := getCenter(srcBounds)
minSize := getMinSide(srcBounds)
minPt := image.Point{}
minPt.X = center.X - (int(minSize) / 2)
minPt.Y = center.Y - (int(minSize) / 2)
maxPt := image.Point{}
maxPt.X = center.X + (int(minSize) / 2)
maxPt.Y = center.Y + (int(minSize) / 2)
return image.Rectangle{
Min: minPt,
Max: maxPt,
}
}
func getCenter(rect image.Rectangle) image.Point {
srcBounds := rect.Bounds()
return image.Point{
X: int(float32(srcBounds.Max.X) / float32(2)),
Y: int(float32(srcBounds.Max.Y) / float32(2)),
}
}
func getMinSide(rect image.Rectangle) int {
srcBounds := rect.Bounds()
height := srcBounds.Max.Y - srcBounds.Min.Y
width := srcBounds.Max.X - srcBounds.Min.X
return int(math.Min(float64(width), float64(height)))
}