mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-27 02:21:34 +00:00
Add PNG and JPEG to WebP transcoding
WebP lossy mode offers up to 30% better compression than JPEG.
This commit is contained in:
parent
7b3424fd18
commit
9518dc2f1a
|
@ -14,7 +14,7 @@ Features:
|
||||||
- gzip compression
|
- gzip compression
|
||||||
- transcode animated gifs to static images
|
- transcode animated gifs to static images
|
||||||
- transcode jpeg images to desired quality using libjpeg
|
- transcode jpeg images to desired quality using libjpeg
|
||||||
- transcode png images
|
- transcode PNG and JPEG images to WebP
|
||||||
- html/css/js minification
|
- html/css/js minification
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package transcoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
|
"github.com/chai2010/webp"
|
||||||
"github.com/pixiv/go-libjpeg/jpeg"
|
"github.com/pixiv/go-libjpeg/jpeg"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -26,8 +27,19 @@ func (t *Jpeg) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, heade
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = jpeg.Encode(w, img, t.encOptions); err != nil {
|
|
||||||
return err
|
if SupportsWebP(headers) {
|
||||||
|
options := webp.Options{
|
||||||
|
Lossless: false,
|
||||||
|
Quality: float32(t.encOptions.Quality),
|
||||||
|
}
|
||||||
|
if err = webp.Encode(w, img, &options); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err = jpeg.Encode(w, img, t.encOptions); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package transcoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/barnacs/compy/proxy"
|
"github.com/barnacs/compy/proxy"
|
||||||
|
"github.com/chai2010/webp"
|
||||||
"image/png"
|
"image/png"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -13,8 +14,18 @@ func (t *Png) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, header
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = png.Encode(w, img); err != nil {
|
|
||||||
return err
|
if SupportsWebP(headers) {
|
||||||
|
options := webp.Options{
|
||||||
|
Lossless: true,
|
||||||
|
}
|
||||||
|
if err = webp.Encode(w, img, &options); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err = png.Encode(w, img); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
15
transcoder/util.go
Normal file
15
transcoder/util.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package transcoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SupportsWebP(headers http.Header) bool {
|
||||||
|
for _, v := range strings.Split(headers.Get("Accept"), ",") {
|
||||||
|
if strings.SplitN(v, ";", 2)[0] == "image/webp" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in a new issue