Add webp image encoding support

Fixes #5924
This commit is contained in:
Bjørn Erik Pedersen
2021-04-07 16:49:34 +02:00
parent 509d39fa6d
commit 33d5f80592
15 changed files with 344 additions and 117 deletions

View File

@@ -23,6 +23,9 @@ import (
"io"
"sync"
"github.com/bep/gowebp/libwebp/webpoptions"
"github.com/gohugoio/hugo/resources/images/webp"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/images/exif"
@@ -89,6 +92,15 @@ func (i *Image) EncodeTo(conf ImageConfig, img image.Image, w io.Writer) error {
case BMP:
return bmp.Encode(w, img)
case WEBP:
return webp.Encode(
w,
img, webpoptions.EncodingOptions{
Quality: conf.Quality,
EncodingPreset: webpoptions.EncodingPreset(conf.Hint),
UseSharpYuv: true,
},
)
default:
return errors.New("format not supported")
}
@@ -229,10 +241,11 @@ func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.
return dst, nil
}
func (p *ImageProcessor) GetDefaultImageConfig(action string) ImageConfig {
func GetDefaultImageConfig(action string, defaults ImagingConfig) ImageConfig {
return ImageConfig{
Action: action,
Quality: p.Cfg.Cfg.Quality,
Hint: defaults.Hint,
Quality: defaults.Cfg.Quality,
}
}
@@ -250,11 +263,13 @@ const (
GIF
TIFF
BMP
WEBP
)
// RequiresDefaultQuality returns if the default quality needs to be applied to images of this format
// RequiresDefaultQuality returns if the default quality needs to be applied to
// images of this format.
func (f Format) RequiresDefaultQuality() bool {
return f == JPEG
return f == JPEG || f == WEBP
}
// SupportsTransparency reports whether it supports transparency in any form.
@@ -281,6 +296,8 @@ func (f Format) MediaType() media.Type {
return media.TIFFType
case BMP:
return media.BMPType
case WEBP:
return media.WEBPType
default:
panic(fmt.Sprintf("%d is not a valid image format", f))
}