forked from nicored/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar_picture.go
56 lines (44 loc) · 1013 Bytes
/
avatar_picture.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
package avatar
import (
"bytes"
"image"
"image/color"
)
type PictureOptions struct {
BgColor color.Color
Size int
}
type Picture struct {
source []byte
options *PictureOptions
originalImage image.Image
}
func (p Picture) originalImg() image.Image {
return p.originalImage
}
func (p Picture) Source() []byte {
return p.source
}
// Generates the square avatar
// It returns the avatar image in []byte or an error something went wrong
func (p Picture) Square() ([]byte, error) {
return square(p, p.options)
}
func (p Picture) Circle() ([]byte, error) {
return circle(p, p.options)
}
func (p Picture) loadOriginalImage() (image.Image, error) {
srcReader := bytes.NewReader(p.source)
// Convert source file to image.Image
originalImage, _, err := image.Decode(srcReader)
if err != nil {
return nil, err
}
return originalImage, nil
}
func (o PictureOptions) bgColor() color.Color {
return bgColor(o.BgColor)
}
func (o PictureOptions) size() int {
return size(o.Size)
}