gif to webp
This commit is contained in:
32
bin/main.go
32
bin/main.go
@@ -2,20 +2,31 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/gif"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/chai2010/webp"
|
"github.com/chai2010/webp"
|
||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
|
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())
|
||||||
|
|
||||||
http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
@@ -65,20 +76,27 @@ func main() {
|
|||||||
|
|
||||||
// 如果是 GIF 格式的动态图片,直接返回
|
// 如果是 GIF 格式的动态图片,直接返回
|
||||||
if ext == "gif" {
|
if ext == "gif" {
|
||||||
giffile, err := os.Open("data/test.gif")
|
gifBin, err := os.ReadFile("data/test.gif")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("打开文件失败", err)
|
log.Println("读取文件失败", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer giffile.Close()
|
|
||||||
gifimg, err := gif.DecodeAll(giffile)
|
converter := giftowebp.NewConverter()
|
||||||
|
converter.LoopCompatibility = false
|
||||||
|
converter.WebPConfig.SetLossless(0) // 0 有损压缩 1无损压缩
|
||||||
|
converter.WebPConfig.SetMethod(6) // 压缩速度 0-6 0最快 6质量最好
|
||||||
|
converter.WebPConfig.SetQuality(10) // 压缩质量 0-100
|
||||||
|
converter.WebPAnimEncoderOptions.SetKmin(9)
|
||||||
|
converter.WebPAnimEncoderOptions.SetKmax(17)
|
||||||
|
webpBuf, err := converter.Convert(gifBin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("解码图像失败", err)
|
log.Println("编码图像失败", err.Error())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gif.EncodeAll(w, gifimg)
|
w.Write(webpBuf)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
go.mod
3
go.mod
@@ -4,9 +4,10 @@ go 1.18
|
|||||||
|
|
||||||
require github.com/disintegration/imaging v1.6.2
|
require github.com/disintegration/imaging v1.6.2
|
||||||
|
|
||||||
require github.com/sizeofint/webpanimation v0.0.0-20210809145948-1d2b32119882 // indirect
|
require github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9 // indirect
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/chai2010/webp v1.1.1
|
github.com/chai2010/webp v1.1.1
|
||||||
|
github.com/sizeofint/gif-to-webp v0.0.0-20210224202734-e9d7ed071591
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
|
||||||
)
|
)
|
||||||
|
7
go.sum
7
go.sum
@@ -2,9 +2,10 @@ github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
|
|||||||
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
|
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
|
||||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||||
github.com/sizeofint/webpanimation v0.0.0-20210809145948-1d2b32119882 h1:A7o8tOERTtpD/poS+2VoassCjXpjHn916luXbf5QKD0=
|
github.com/sizeofint/gif-to-webp v0.0.0-20210224202734-e9d7ed071591 h1:dCWBD4Xchp/XFIR/x6D2l74DtQHvIpHsmpPRHgH9oUo=
|
||||||
github.com/sizeofint/webpanimation v0.0.0-20210809145948-1d2b32119882/go.mod h1:5IwJoz9Pw7JsrCN4/skkxUtSWT7myuUPLhCgv6Q5vvQ=
|
github.com/sizeofint/gif-to-webp v0.0.0-20210224202734-e9d7ed071591/go.mod h1:IXC7KN2FEuTEISdePm37qcFyXInAh6pfW35yDjbdfOM=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9 h1:i3LYMwQ0zkh/BJ47vIZN+jBYqV4/f6DFoAsW8rwV490=
|
||||||
|
github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9/go.mod h1:/NQ8ciRuH+vxYhrFlnX70gvXBugMYQbBygCRocFgSZ4=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
||||||
|
Reference in New Issue
Block a user