]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
images: Add some more PNG tests
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 20 Dec 2025 10:57:34 +0000 (11:57 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 20 Dec 2025 12:42:21 +0000 (13:42 +0100)
To try to figure out #14288.

tpl/images/images_integration_test.go
tpl/images/images_test.go

index 59cdd0f46d4dd12c8905a50a08b014ada2e2a8ae..c6226f611a8e08924aa69e80eb8c094b67a10409 100644 (file)
@@ -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
+`)
+}
index 4f656d1f940db5afcdb766093358254e97db59e2..be5db69fe993b8d33d836096eab0d6c3dbcfcfeb 100644 (file)
@@ -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()