resources/images: Fix 2 animated GIF resize issues

* Fix resize of animated GIF when target != GIF
* Avoid processing all GIF frames if targetFormat != GIF

Fixes #10354
This commit is contained in:
Bjørn Erik Pedersen 2022-10-04 11:00:07 +02:00
parent 0addb302ac
commit 3a9cb7b0fb
8 changed files with 28 additions and 16 deletions

View file

@ -620,6 +620,8 @@ func TestImageOperationsGolden(t *testing.T) {
// Note, if you're enabling this on a MacOS M1 (ARM) you need to run the test with GOARCH=amd64. // Note, if you're enabling this on a MacOS M1 (ARM) you need to run the test with GOARCH=amd64.
// GOARCH=amd64 go test -timeout 30s -run "^TestImageOperationsGolden$" ./resources -v // GOARCH=amd64 go test -timeout 30s -run "^TestImageOperationsGolden$" ./resources -v
// The above will print out a folder.
// Replace testdata/golden with resources/_gen/images in that folder.
devMode := false devMode := false
testImages := []string{"sunset.jpg", "gohugoio8.png", "gohugoio24.png"} testImages := []string{"sunset.jpg", "gohugoio8.png", "gohugoio24.png"}
@ -663,7 +665,7 @@ func TestImageOperationsGolden(t *testing.T) {
// Animated GIF // Animated GIF
orig = fetchImageForSpec(spec, c, "giphy.gif") orig = fetchImageForSpec(spec, c, "giphy.gif")
for _, resizeSpec := range []string{"200x", "512x"} { for _, resizeSpec := range []string{"200x", "512x", "100x jpg"} {
resized, err := orig.Resize(resizeSpec) resized, err := orig.Resize(resizeSpec)
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
rel := resized.RelPermalink() rel := resized.RelPermalink()

View file

@ -60,6 +60,7 @@ var (
imageFormatsVersions = map[Format]int{ imageFormatsVersions = map[Format]int{
PNG: 3, // Fix transparency issue with 32 bit images. PNG: 3, // Fix transparency issue with 32 bit images.
WEBP: 2, // Fix transparency issue with 32 bit images. WEBP: 2, // Fix transparency issue with 32 bit images.
GIF: 1, // Fix resize issue with animated GIFs when target != GIF.
} }
// Increment to mark all processed images as stale. Only use when absolutely needed. // Increment to mark all processed images as stale. Only use when absolutely needed.

View file

@ -247,7 +247,7 @@ func (p *ImageProcessor) ApplyFiltersFromConfig(src image.Image, conf ImageConfi
return nil, fmt.Errorf("unsupported action: %q", conf.Action) return nil, fmt.Errorf("unsupported action: %q", conf.Action)
} }
img, err := p.Filter(src, filters...) img, err := p.doFilter(src, conf.TargetFormat, filters...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -256,11 +256,18 @@ func (p *ImageProcessor) ApplyFiltersFromConfig(src image.Image, conf ImageConfi
} }
func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.Image, error) { func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.Image, error) {
return p.doFilter(src, 0, filters...)
}
func (p *ImageProcessor) doFilter(src image.Image, targetFormat Format, filters ...gift.Filter) (image.Image, error) {
filter := gift.New(filters...) filter := gift.New(filters...)
if giph, ok := src.(Giphy); ok && len(giph.GIF().Image) > 1 { if giph, ok := src.(Giphy); ok {
g := giph.GIF() g := giph.GIF()
if len(g.Image) < 2 || (targetFormat == 0 || targetFormat != GIF) {
src = g.Image[0]
} else {
var bounds image.Rectangle var bounds image.Rectangle
firstFrame := g.Image[0] firstFrame := g.Image[0]
tmp := image.NewNRGBA(firstFrame.Bounds()) tmp := image.NewNRGBA(firstFrame.Bounds())
@ -277,6 +284,8 @@ func (p *ImageProcessor) Filter(src image.Image, filters ...gift.Filter) (image.
return giph, nil return giph, nil
} }
}
bounds := filter.Bounds(src.Bounds()) bounds := filter.Bounds(src.Bounds())
var dst draw.Image var dst draw.Image

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB