From 80ea90c880e805814d6689769c4a1969f592f539 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 20 Dec 2025 11:57:34 +0100 Subject: [PATCH] images: Add some more PNG tests To try to figure out #14288. --- tpl/images/images_integration_test.go | 23 +++++++++++++++++++++++ tpl/images/images_test.go | 27 ++++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/tpl/images/images_integration_test.go b/tpl/images/images_integration_test.go index 59cdd0f46..c6226f611 100644 --- a/tpl/images/images_integration_test.go +++ b/tpl/images/images_integration_test.go @@ -142,3 +142,26 @@ Home. imagetesting.RunGolden(opts) } + +func TestPNGIssue14288(t *testing.T) { + t.Parallel() + + files := ` +-- assets/qr.png -- +sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png +-- layouts/home.html -- +{{ $img := resources.Get "qr.png" }} +images.Config: {{ (images.Config "assets/qr.png").Width }} +Resize to 100x100: {{ ($img.Resize "100x100" ).Width }} +Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }} +Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }} +` + + b := hugolib.Test(t, files) + b.AssertFileContent("public/index.html", ` +images.Config: 222 +Resize to 100x100: 100 +Brightnes method: /qr_hu_d5f06fd7594d0594.png +Brightnes func: /qr_hu_d5f06fd7594d0594.png +`) +} diff --git a/tpl/images/images_test.go b/tpl/images/images_test.go index 4f656d1f9..be5db69fe 100644 --- a/tpl/images/images_test.go +++ b/tpl/images/images_test.go @@ -17,27 +17,32 @@ import ( "bytes" "image" "image/color" - "image/png" "path/filepath" "testing" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/config/testconfig" + "github.com/gohugoio/hugo/resources/images" "github.com/spf13/afero" "github.com/spf13/cast" ) type tstNoStringer struct{} +type widthHeight struct { + Width int + Height int +} + var configTests = []struct { path any - input []byte + input widthHeight expect any }{ { path: "a.png", - input: blankImage(10, 10), + input: widthHeight{10, 10}, expect: image.Config{ Width: 10, Height: 10, @@ -46,7 +51,7 @@ var configTests = []struct { }, { path: "a.png", - input: blankImage(10, 10), + input: widthHeight{10, 10}, expect: image.Config{ Width: 10, Height: 10, @@ -55,7 +60,7 @@ var configTests = []struct { }, { path: "b.png", - input: blankImage(20, 15), + input: widthHeight{20, 15}, expect: image.Config{ Width: 20, Height: 15, @@ -64,7 +69,7 @@ var configTests = []struct { }, { path: "a.png", - input: blankImage(20, 15), + input: widthHeight{20, 15}, expect: image.Config{ Width: 10, Height: 10, @@ -101,7 +106,8 @@ func TestNSConfig(t *testing.T) { // cast path to string for afero.WriteFile sp, err := cast.ToStringE(test.path) c.Assert(err, qt.IsNil) - afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), test.input, 0o755) + img := blankImage(d.ResourceSpec.Imaging.Codec, test.input.Width, test.input.Height) + afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), img, 0o755) result, err := ns.Config(test.path) @@ -111,10 +117,13 @@ func TestNSConfig(t *testing.T) { } } -func blankImage(width, height int) []byte { +func blankImage(codec *images.Codec, width, height int) []byte { var buf bytes.Buffer img := image.NewRGBA(image.Rect(0, 0, width, height)) - if err := png.Encode(&buf, img); err != nil { + cfg := images.ImageConfig{ + TargetFormat: images.PNG, + } + if err := codec.EncodeTo(cfg, &buf, img); err != nil { panic(err) } return buf.Bytes() -- 2.39.5