-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (116 loc) · 2.47 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
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
package generate
import (
"fmt"
"image"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"os"
"github.com/esimov/stackblur-go"
"github.com/oliamb/cutter"
"github.com/valyala/fastrand"
)
func getImageSize(imgPath string) *image.Config {
fmt.Println(imgPath)
file, err := os.Open(imgPath)
defer file.Close()
if err != nil {
fmt.Println(err)
return nil
}
image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Println(err)
return nil
}
return &image
}
func getCropCoordinate(originalWidth, originalHeight, cropWidth, cropHeight uint32) (uint, uint) {
var minX uint32 = 0
var minY uint32 = 0
var maxX uint32 = originalWidth - cropWidth
var maxY uint32 = originalHeight - cropHeight
x := fastrand.Uint32n(maxX-minX) + minX
y := fastrand.Uint32n(maxY-minY) + minY
return uint(x), uint(y)
}
func cropImage(path string, width uint, height uint, x uint, y uint) (image.Image, error) {
fmt.Println(path, width, height, x, y)
file, err := os.Open(path)
defer file.Close()
if err != nil {
fmt.Println(err)
return nil, err
}
img, _, err := image.Decode(file)
if err != nil {
fmt.Println(err)
return nil, err
}
croppedImg, err := cutter.Crop(img, cutter.Config{
Width: int(width),
Height: int(height),
Anchor: image.Point{int(x), int(y)},
Mode: cutter.TopLeft, // optional, default value
})
return croppedImg, err
}
func blurImage() {
}
func SaveJPEGImage(img image.Image, path string) {
f, err := os.Create(path)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
opt := jpeg.Options{
Quality: 90,
}
err = jpeg.Encode(f, img, &opt)
if err != nil {
fmt.Println(err)
return
}
}
type CropConfig struct {
Width uint
Height uint
Blur uint
}
func GenerateFrom(imgPath string, cropConfig *CropConfig) image.Image {
imageConfig := getImageSize(imgPath)
var cropWidth uint = cropConfig.Width
var cropHeight uint = cropConfig.Height
originalWidth := imageConfig.Width
originalHeight := imageConfig.Height
x, y := getCropCoordinate(
uint32(originalWidth),
uint32(originalHeight),
uint32(cropWidth),
uint32(cropHeight),
)
img, err := cropImage(
imgPath,
cropWidth,
cropHeight,
x,
y,
)
if err != nil {
panic(err)
}
if cropConfig.Blur > 0 {
var done chan struct{} = make(chan struct{}, uint32(cropConfig.Blur))
img = stackblur.Process(
img,
uint32(cropConfig.Width),
uint32(cropConfig.Height),
uint32(cropConfig.Blur),
done,
)
<-done
}
return img
}