Add GIF to WebP transcoding

This commit is contained in:
Andrew Gaul 2017-01-19 11:38:48 -08:00
parent f69a0b7e0e
commit e505655338
2 changed files with 28 additions and 2 deletions

View file

@ -134,6 +134,21 @@ func (s *CompyTest) TestGif(c *C) {
c.Assert(uncompressedLength > compressedLength, Equals, true)
}
func (s *CompyTest) TestGifToWebP(c *C) {
req, err := http.NewRequest("GET", s.server.URL+"/image/gif", nil)
c.Assert(err, IsNil)
req.Header.Add("Accept", "image/webp")
resp, err := s.client.Do(req)
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, 200)
c.Assert(resp.Header.Get("Content-Type"), Equals, "image/webp")
_, err = webp.Decode(resp.Body)
c.Assert(err, IsNil)
}
func (s *CompyTest) TestJpeg(c *C) {
resp, err := s.client.Get(s.server.URL + "/image/jpeg")
c.Assert(err, IsNil)

View file

@ -2,6 +2,7 @@ package transcoder
import (
"github.com/barnacs/compy/proxy"
"github.com/chai2010/webp"
"image/gif"
"net/http"
)
@ -13,8 +14,18 @@ func (t *Gif) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, header
if err != nil {
return err
}
if SupportsWebP(headers) {
w.Header().Set("Content-Type", "image/webp")
options := webp.Options{
Lossless: true,
}
if err = webp.Encode(w, img, &options); err != nil {
return err
}
} else {
if err = gif.Encode(w, img, nil); err != nil {
return err
}
}
return nil
}