-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage.go
128 lines (101 loc) · 3.18 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"image/png"
"log"
"strings"
"github.com/chai2010/webp"
"github.com/lucasb-eyer/go-colorful"
"github.com/nfnt/resize"
)
// 图片处理函数,获取主题色调等
func handleImageRequest(imageData []byte, params string) (json.RawMessage, string) {
if strings.Contains(params, "get=theme") {
theme, err := getThemeColor(imageData)
if err != nil {
log.Printf("获取图片主题色调时出错:%v\n", err)
errorJSON := fmt.Sprintf(`{"error": "主题色调获取失败", "hitokoto": "%s"}`, hitokoto())
return nil, errorJSON
}
return json.RawMessage(fmt.Sprintf(`{"theme":"%s"}`, theme)), "application/json"
}
if strings.Contains(params, "get=size") {
img, _, err := image.Decode(bytes.NewReader(imageData))
if err != nil {
log.Printf("获取图片尺寸时出错:%v\n", err)
errorJSON := fmt.Sprintf(`{"error": "图片尺寸获取失败", "hitokoto": "%s"}`, hitokoto())
return nil, errorJSON
}
bounds := img.Bounds()
width := bounds.Dx()
height := bounds.Dy()
return json.RawMessage(fmt.Sprintf(`{"width":%d,"height":%d}`, width, height)), "application/json"
}
return nil, ""
}
// img2color
func getThemeColor(imageData []byte) (string, error) {
img, _, err := image.Decode(bytes.NewReader(imageData))
if err != nil {
return "", err
}
img = resize.Resize(50, 0, img, resize.Lanczos3)
bounds := img.Bounds()
var r, g, b uint32
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := img.At(x, y)
r0, g0, b0, _ := c.RGBA()
r += r0
g += g0
b += b0
}
}
totalPixels := uint32(bounds.Dx() * bounds.Dy())
averageR := r / totalPixels
averageG := g / totalPixels
averageB := b / totalPixels
mainColor := colorful.Color{R: float64(averageR) / 0xFFFF, G: float64(averageG) / 0xFFFF, B: float64(averageB) / 0xFFFF}
colorHex := mainColor.Hex()
return colorHex, nil
}
func convertWebpToPng(webpData []byte) ([]byte, error) {
img, err := webp.Decode(bytes.NewReader(webpData))
if err != nil {
return nil, fmt.Errorf("解码webp图像失败:%v", err)
}
buf := new(bytes.Buffer)
err = png.Encode(buf, img)
if err != nil {
return nil, fmt.Errorf("转换图片格式失败:%v", err)
}
return buf.Bytes(), nil
}
func compressImage(imageData []byte) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(imageData))
if err != nil {
return nil, fmt.Errorf("解码图像失败:%v", err)
}
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, img, &jpeg.Options{Quality: 80})
if err != nil {
return nil, fmt.Errorf("压缩图片失败:%v", err)
}
return buf.Bytes(), nil
}
func convertImageToWebp(imageData []byte) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(imageData))
if err != nil {
return nil, fmt.Errorf("解码图像失败:%v", err)
}
buf := new(bytes.Buffer)
err = webp.Encode(buf, img, &webp.Options{Lossless: false, Quality: 80})
if err != nil {
return nil, fmt.Errorf("转换图片格式失败:%v", err)
}
return buf.Bytes(), nil
}