forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
241 lines (197 loc) · 4.65 KB
/
image.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package utils
import (
"chat/globals"
"fmt"
"image"
"image/gif"
"image/jpeg"
"io"
"math"
"net/http"
"os"
"path"
"strings"
"github.com/chai2010/webp"
)
type Image struct {
Object image.Image
Content string
}
type Images []Image
func NewImage(url string) (*Image, error) {
if strings.HasPrefix(url, "data:image/") {
data := SafeSplit(url, ",", 2)
if data[1] == "" {
return nil, nil
}
decoded, err := Base64Decode(data[1])
if err != nil {
return nil, err
}
img, _, err := image.Decode(strings.NewReader(string(decoded)))
if err != nil {
return nil, err
}
return &Image{Object: img, Content: url}, nil
}
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
var img image.Image
suffix := strings.ToLower(path.Ext(url))
switch suffix {
case ".png":
if img, _, err = image.Decode(res.Body); err != nil {
return nil, err
}
case ".jpg", ".jpeg":
if img, err = jpeg.Decode(res.Body); err != nil {
return nil, err
}
case "webp":
if img, err = webp.Decode(res.Body); err != nil {
return nil, err
}
case "gif":
ticks, err := gif.DecodeAll(res.Body)
if err != nil {
return nil, err
}
img = ticks.Image[0]
}
return &Image{Object: img, Content: url}, nil
}
func NewImageContent(content string) *Image {
return &Image{Content: content}
}
func ConvertToBase64(url string) (string, error) {
if strings.HasPrefix(url, "data:image/") {
data := strings.Split(url, ",")
if len(data) != 2 {
return "", nil
}
return data[1], nil
}
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return Base64EncodeBytes(data), nil
}
func (i *Image) GetWidth() int {
return i.Object.Bounds().Max.X
}
func (i *Image) GetHeight() int {
return i.Object.Bounds().Max.Y
}
func (i *Image) GetPixel(x int, y int) (uint32, uint32, uint32, uint32) {
return i.Object.At(x, y).RGBA()
}
func (i *Image) GetPixelColor(x int, y int) (int, int, int) {
r, g, b, _ := i.GetPixel(x, y)
return int(r), int(g), int(b)
}
func (i *Image) CountTokens(model string) int {
if globals.IsVisionModel(model) {
// tile size is 512x512
// the max size of image is 2048x2048
// the image that is larger than 2048x2048 will be resized in 16 tiles
x := LimitMax(math.Ceil(float64(i.GetWidth())/512), 4)
y := LimitMax(math.Ceil(float64(i.GetHeight())/512), 4)
tiles := int(x) * int(y)
return 85 + 170*tiles
}
return 0
}
func (i *Image) IsBase64() bool {
return strings.HasPrefix(i.Content, "data:image/")
}
func (i *Image) GetType() string {
// example: image/jpeg, image/png, image/gif
if i.IsBase64() {
t := SafeSplit(i.Content, ";", 2)[0]
return strings.ReplaceAll(t, "data:", "")
}
// example: .jpg, .png, .gif to image/jpeg, image/png, image/gif
switch strings.ToLower(path.Ext(i.Content)) {
case ".jpg", ".jpeg":
return "image/jpeg"
case ".png":
return "image/png"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".bmp":
return "image/bmp"
default:
return ""
}
}
func (i *Image) ToBase64() string {
if i.IsBase64() {
return i.Content
}
// get url content and convert to base64
data, err := ConvertToBase64(i.Content)
if err != nil {
globals.Warn(fmt.Sprintf("cannot convert image to base64: %s", err.Error()))
return ""
}
return fmt.Sprintf("data:%s;base64,%s", i.GetType(), data)
}
func (i *Image) ToRawBase64() string {
// example: return /9j/...
if i.IsBase64() {
return SafeSplit(i.Content, ",", 2)[1]
}
// get url content and convert to base64
data, err := ConvertToBase64(i.Content)
if err != nil {
globals.Warn(fmt.Sprintf("cannot convert image to base64: %s", err.Error()))
return ""
}
return data
}
func DownloadImage(url string, path string) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
globals.Debug("[utils] close file error: %s (path: %s)", err.Error(), path)
}
}(res.Body)
file, err := os.Create(path)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Debug("[utils] close file error: %s (path: %s)", err.Error(), path)
}
}(file)
_, err = io.Copy(file, res.Body)
return err
}
func StoreImage(url string) string {
if globals.AcceptImageStore {
hash := Md5Encrypt(url) + path.Ext(url)
if err := DownloadImage(url, fmt.Sprintf("storage/attachments/%s", hash)); err != nil {
globals.Warn(fmt.Sprintf("[utils] save image error: %s", err.Error()))
return url
}
return fmt.Sprintf("%s/attachments/%s", globals.NotifyUrl, hash)
}
return url
}