-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (140 loc) · 3.06 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
package main
import (
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
"github.com/kolesa-team/go-webp/encoder"
"github.com/kolesa-team/go-webp/webp"
"image"
"log"
"strconv"
"strings"
)
func main() {
engine := gin.Default()
engine.POST("/webp/*quality", func(ctx *gin.Context) {
pressParam := strings.ToLower(ctx.Param("quality"))
split := strings.Split(strings.TrimPrefix(pressParam, "/"), "/")
var resizedWidth int
var resizedHeight int
var lossless bool
var quality float32
var level int
for _, s := range split {
if len(s) == 0 {
continue
}
if s == "lossless" {
lossless = true
continue
}
cmd := s[0]
switch cmd {
case 's':
p := s[1:]
index := strings.Index(p, "*")
if index == -1 {
Error(ctx, "size param error")
return
}
widthStr := p[:index]
heightStr := p[index+1:]
width, err := strconv.Atoi(widthStr)
if err != nil {
Error(ctx, "size param error")
return
}
height, err := strconv.Atoi(heightStr)
if err != nil {
Error(ctx, "size param error")
return
}
resizedWidth = width
resizedHeight = height
case 'q': //quality
q, err := strconv.Atoi(s[1:])
if err != nil {
Error(ctx, "quality param error")
return
}
quality = float32(q)
case 'l': //level
l, err := strconv.Atoi(s[1:])
if err != nil {
Error(ctx, "level param error")
return
}
level = l
}
}
var options *encoder.Options
if lossless {
encoderOptions, err := encoder.NewLosslessEncoderOptions(encoder.PresetDefault, level)
if err != nil {
Error(ctx, "param error")
return
}
options = encoderOptions
} else {
encoderOptions, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, quality)
if err != nil {
log.Println(err.Error())
Error(ctx, "param error")
return
}
options = encoderOptions
}
options.LowMemory = true
options.Quality = quality
file, err := ctx.FormFile("file")
if err != nil {
log.Println(err.Error())
Error(ctx, "param error")
return
}
open, err := file.Open()
if err != nil {
log.Println(err.Error())
Error(ctx, "network error")
return
}
decode, err := imaging.Decode(open)
resized := resizeImg(decode, resizedWidth, resizedHeight)
decode = nil
if err != nil {
log.Println(err.Error())
Error(ctx, "decode image error")
return
}
ctx.Writer.Header().Add("Content-Type", "image/webp")
err = webp.Encode(ctx.Writer, resized, options)
if err != nil {
log.Println(err.Error())
Error(ctx, "encode image error")
return
}
})
err := engine.Run(":8889")
if err != nil {
panic(err.Error())
}
}
func resizeImg(img image.Image, width int, height int) image.Image {
x := img.Bounds().Size().X
y := img.Bounds().Size().Y
if width == 0 || height == 0 {
return img
}
var w = x
var h = y
if x >= y && x > width {
w = width
h = y * width / x
} else if y >= x && y > height {
h = height
w = x * height / y
}
if w != x || h != y {
return imaging.Resize(img, w, h, imaging.Lanczos)
}
return img
}