add bmp and gif support.

gif support is not perfect, need animated webp
This commit is contained in:
Benny~ 2020-03-03 19:26:48 +08:00
parent 2fa8db6b02
commit 102cba7f4f
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
3 changed files with 17 additions and 6 deletions

View File

@ -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"
]
}

1
go.mod
View File

@ -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
)

View File

@ -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"