mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
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:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user