webp_server_go/encoder.go

73 lines
1.5 KiB
Go

package main
import (
"bytes"
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"path"
"strings"
log "github.com/sirupsen/logrus"
"github.com/chai2010/webp"
"golang.org/x/image/bmp"
)
func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
// if convert fails, return error; success nil
log.Debugf("target: %s with quality of %f", path.Base(p1), quality)
var buf bytes.Buffer
var img image.Image
data, err := ioutil.ReadFile(p1)
if err != nil {
ChanErr(c)
return
}
contentType := GetFileContentType(data[:512])
if strings.Contains(contentType, "jpeg") {
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
log.Warn("Gif support is not perfect!")
img, _ = gif.Decode(bytes.NewReader(data))
}
if img == nil {
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
log.Debug(msg)
err = errors.New(msg)
ChanErr(c)
return
}
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
log.Error(err)
ChanErr(c)
return
}
if err = ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {
log.Error(err)
ChanErr(c)
return
}
if Log {
log.Info("Save to " + p2 + " ok!\n")
}
ChanErr(c)
return nil
}