-
Notifications
You must be signed in to change notification settings - Fork 249
/
main.go
103 lines (83 loc) · 1.99 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
package images
import (
"bytes"
"image"
)
func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
var iis []IdentityImage
var err error
for _, s := range ResizeDimensions {
rImg := Resize(s, cImg)
bb := bytes.NewBuffer([]byte{})
err = EncodeToBestSize(bb, rImg, s)
if err != nil {
return nil, err
}
ii := IdentityImage{
Name: ResizeDimensionToName[s],
Payload: bb.Bytes(),
Width: rImg.Bounds().Dx(),
Height: rImg.Bounds().Dy(),
FileSize: bb.Len(),
ResizeTarget: int(s),
}
iis = append(iis, ii)
}
return iis, nil
}
func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]IdentityImage, error) {
img, err := Decode(filepath)
if err != nil {
return nil, err
}
cropRect := image.Rectangle{
Min: image.Point{X: aX, Y: aY},
Max: image.Point{X: bX, Y: bY},
}
cImg, err := Crop(img, cropRect)
if err != nil {
return nil, err
}
return GenerateImageVariants(cImg)
}
func GenerateIdentityImagesFromURL(url string) ([]IdentityImage, error) {
img, err := DecodeFromURL(url)
if err != nil {
return nil, err
}
cImg, err := CropCenter(img)
if err != nil {
return nil, err
}
return GenerateImageVariants(cImg)
}
func GenerateBannerImage(filepath string, aX, aY, bX, bY int) (*IdentityImage, error) {
img, err := Decode(filepath)
if err != nil {
return nil, err
}
cropRect := image.Rectangle{
Min: image.Point{X: aX, Y: aY},
Max: image.Point{X: bX, Y: bY},
}
croppedImg, err := Crop(img, cropRect)
if err != nil {
return nil, err
}
resizedImg := ShrinkOnly(BannerDim, croppedImg)
sizeLimits := GetBannerDimensionLimits()
bb := bytes.NewBuffer([]byte{})
err = EncodeToLimits(bb, resizedImg, sizeLimits)
if err != nil {
return nil, err
}
ii := &IdentityImage{
Name: BannerIdentityName,
Payload: bb.Bytes(),
Width: resizedImg.Bounds().Dx(),
Height: resizedImg.Bounds().Dy(),
FileSize: bb.Len(),
ResizeTarget: int(BannerDim),
}
return ii, nil
}