-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
124 lines (118 loc) · 3.39 KB
/
process.go
File metadata and controls
124 lines (118 loc) · 3.39 KB
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
package imageprocess
import (
"image"
"image/gif"
"io"
"strings"
)
func Process(img image.Image, w io.Writer, originalFormat Format, options []Option) error {
//循环 options 处理
img, format, quality, err := processImage(img, options)
if err != nil {
return err
}
//format and quality
if format == "" {
format = originalFormat
}
if quality == 0 {
quality = 100
}
return EncodeImage(img, w, format, quality)
}
func ProcessGif(gifImg *gif.GIF, w io.Writer, options []Option) error {
isGif := true
parameterTypes := getParameterTypes(options)
if contains(parameterTypes, string(FormatType)) {
parmsStr := SerializeOptions(options)
if !strings.Contains(parmsStr, "gif") {
// 不转换为gif
isGif = false
}
}
// 是否转换为GIF
if isGif {
// 遍历GIF的每一帧进行缩放
for i := range gifImg.Image {
original := gifImg.Image[i]
// 使用nfnt/resize库对图像进行缩放
resized, _, _, err := processImage(original, options)
if err != nil {
return err
}
// 将缩放后的图像替换掉原图像
gifImg.Image[i] = image.NewPaletted(resized.Bounds(), original.Palette)
for y := 0; y < resized.Bounds().Dy(); y++ {
for x := 0; x < resized.Bounds().Dx(); x++ {
gifImg.Image[i].Set(x, y, resized.At(x, y))
}
}
// 更新每一帧的尺寸
gifImg.Config.Width = int(resized.Bounds().Dx())
gifImg.Config.Height = int(resized.Bounds().Dy())
}
return gif.EncodeAll(w, gifImg)
} else {
return Process(gifImg.Image[0], w, GIF, options)
}
}
func processImage(img image.Image, options []Option) (image.Image, Format, int, error) {
var format Format
var quality int
for _, option := range options {
switch option.Parameter {
case Resize:
img = ResizeImage(img, option.Option.(ResizeOption))
case Crop:
img = CropImage(img, option.Option.(CropOption))
case Watermark:
img = WarterMarkText(img, option.Option.(TextWatermarkOption))
case Rotate:
img = AddjustRotate(img, option.Option.(RotateOption))
case Blur:
img = AdjustBlur(img, option.Option.(BlurOption))
case Sharpen:
img = AddjustSharpen(img, option.Option.(SharpenOption))
case Saturation:
img = AddjustSaturation(img, option.Option.(SaturationOption))
case Gamma:
img = AddjustGamma(img, option.Option.(GammaOption))
case Brightness:
img = AddjustBright(img, option.Option.(BrightnessOption))
case Contrast:
img = AddjustContrast(img, option.Option.(ContrastOption))
case FormatType:
format = option.Option.(FormatOption).Format
case Quality:
quality = option.Option.(QualityOption).Quality
default:
return nil, "", 100, ErrUnsupportedParameter
}
}
return img, format, quality, nil
}
// getParameterTypes 接收一个Option类型的切片,并返回一个Parameter类型的切片。
// 该函数的目的是从提供的Option中提取出所有的parameter。
//
// 参数:
//
// options - 一个包含多个Option的切片,每个Option都嵌有一个Parameter。
//
// 返回值:
//
// 一个Parameter类型的切片,包含了从options中提取出的所有parameter。
func getParameterTypes(options []Option) []string {
parameterTypes := make([]string, 0)
for _, option := range options {
parameterTypes = append(parameterTypes, string(option.Parameter))
}
return parameterTypes
}
func contains(arr []string, target string) bool {
for _, val := range arr {
if val == target {
return true
}
}
return false
}