From 102cba7f4fd1b04edf8380acdacf03f089b0f5d8 Mon Sep 17 00:00:00 2001 From: Benny~ Date: Tue, 3 Mar 2020 19:26:48 +0800 Subject: [PATCH] add bmp and gif support. gif support is not perfect, need animated webp --- config.json | 12 +++++++++--- go.mod | 1 + webp-server.go | 10 +++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index 65cc745..9b3a97a 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,13 @@ "HOST": "127.0.0.1", "PORT": "3333", "QUALITY": "80", - "IMG_PATH": "/path/to/pics", - "EXHAUST_PATH": "/path/to/exhaust", - "ALLOWED_TYPES": ["jpg","png","jpeg"] + "IMG_PATH": "/Users/benny/goLandProject/webp_server_go/pics", + "EXHAUST_PATH": "", + "ALLOWED_TYPES": [ + "jpg", + "png", + "jpeg", + "bmp", + "gif" + ] } diff --git a/go.mod b/go.mod index 1500dc3..dc9e825 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.13 require ( github.com/chai2010/webp v1.1.0 github.com/gofiber/fiber v1.4.0 + golang.org/x/image v0.0.0-20200119044424-58c23975cae1 ) diff --git a/webp-server.go b/webp-server.go index 67f9629..05330d2 100644 --- a/webp-server.go +++ b/webp-server.go @@ -7,7 +7,9 @@ import ( "errors" "flag" "fmt" + "golang.org/x/image/bmp" "image" + "image/gif" "image/jpeg" "image/png" "io/ioutil" @@ -84,10 +86,12 @@ func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro img, _ = jpeg.Decode(bytes.NewReader(data)) } else if strings.Contains(contentType, "png") { img, _ = png.Decode(bytes.NewReader(data)) + } else if strings.Contains(contentType, "bmp") { + img, _ = bmp.Decode(bytes.NewReader(data)) + } else if strings.Contains(contentType, "gif") { + // TODO: need to support animated webp + img, _ = gif.Decode(bytes.NewReader(data)) } - // TODO should we add bmp and tiff support? Need more packages. - // import "golang.org/x/image/bmp" - // import "golang.org/x/image/tiff" if img == nil { msg := "image file " + path.Base(p1) + " is corrupted or not supported"