Add $image.Process

Which supports all the existing actions: resize, crop, fit, fill.

But it also allows plain format conversions:

```
{{ $img = $img.Process "webp" }}
```

Which will be a simple re-encoding of the source image.

Fixes #11483
This commit is contained in:
Bjørn Erik Pedersen
2023-09-22 15:15:16 +02:00
parent c32094ace1
commit ef0e7149d6
13 changed files with 216 additions and 98 deletions

View File

@@ -14,6 +14,7 @@
package images
import (
"errors"
"fmt"
"image"
"image/color"
@@ -35,8 +36,6 @@ import (
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
"errors"
"github.com/gohugoio/hugo/common/hugio"
)
@@ -245,7 +244,11 @@ func (p *ImageProcessor) ApplyFiltersFromConfig(src image.Image, conf ImageConfi
case "fit":
filters = append(filters, gift.ResizeToFit(conf.Width, conf.Height, conf.Filter))
default:
return nil, fmt.Errorf("unsupported action: %q", conf.Action)
}
if len(filters) == 0 {
return p.resolveSrc(src, conf.TargetFormat), nil
}
img, err := p.doFilter(src, conf.TargetFormat, filters...)
@@ -260,8 +263,17 @@ func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.
return p.doFilter(src, 0, filters...)
}
func (p *ImageProcessor) doFilter(src image.Image, targetFormat Format, filters ...gift.Filter) (image.Image, error) {
func (p *ImageProcessor) resolveSrc(src image.Image, targetFormat Format) image.Image {
if giph, ok := src.(Giphy); ok {
g := giph.GIF()
if len(g.Image) < 2 || (targetFormat == 0 || targetFormat != GIF) {
src = g.Image[0]
}
}
return src
}
func (p *ImageProcessor) doFilter(src image.Image, targetFormat Format, filters ...gift.Filter) (image.Image, error) {
filter := gift.New(filters...)
if giph, ok := src.(Giphy); ok {