变量复用
This commit is contained in:
19
bin/main.go
19
bin/main.go
@@ -15,15 +15,6 @@ import (
|
|||||||
giftowebp "github.com/sizeofint/gif-to-webp"
|
giftowebp "github.com/sizeofint/gif-to-webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Image struct {
|
|
||||||
FilePath string
|
|
||||||
Data []byte
|
|
||||||
ImageType int
|
|
||||||
Ext string
|
|
||||||
Width int
|
|
||||||
Height int
|
|
||||||
}
|
|
||||||
|
|
||||||
// 实现一个 web api 服务(获取指定尺寸的图片)
|
// 实现一个 web api 服务(获取指定尺寸的图片)
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
@@ -76,22 +67,20 @@ func main() {
|
|||||||
|
|
||||||
// 如果是 GIF 格式的动态图片,直接返回
|
// 如果是 GIF 格式的动态图片,直接返回
|
||||||
if ext == "gif" {
|
if ext == "gif" {
|
||||||
gifBin, err := os.ReadFile("data/test.gif")
|
var gifBin, webpBuf []byte
|
||||||
if err != nil {
|
if gifBin, err = os.ReadFile("data/test.gif"); err != nil {
|
||||||
log.Println("读取文件失败", err)
|
log.Println("读取文件失败", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
converter := giftowebp.NewConverter()
|
converter := giftowebp.NewConverter()
|
||||||
converter.LoopCompatibility = false
|
converter.LoopCompatibility = false // 是否兼容循环动画
|
||||||
converter.WebPConfig.SetLossless(0) // 0 有损压缩 1无损压缩
|
converter.WebPConfig.SetLossless(0) // 0 有损压缩 1无损压缩
|
||||||
converter.WebPConfig.SetMethod(6) // 压缩速度 0-6 0最快 6质量最好
|
converter.WebPConfig.SetMethod(6) // 压缩速度 0-6 0最快 6质量最好
|
||||||
converter.WebPConfig.SetQuality(10) // 压缩质量 0-100
|
converter.WebPConfig.SetQuality(10) // 压缩质量 0-100
|
||||||
converter.WebPAnimEncoderOptions.SetKmin(9)
|
converter.WebPAnimEncoderOptions.SetKmin(9)
|
||||||
converter.WebPAnimEncoderOptions.SetKmax(17)
|
converter.WebPAnimEncoderOptions.SetKmax(17)
|
||||||
webpBuf, err := converter.Convert(gifBin)
|
if webpBuf, err = converter.Convert(gifBin); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Println("编码图像失败", err.Error())
|
log.Println("编码图像失败", err.Error())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user