mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-18 22:29:22 +00:00
Add GIF to WebP transcoding
This commit is contained in:
parent
f69a0b7e0e
commit
e505655338
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue