Skip to content

Commit 4085ee9

Browse files
committed
Handle PNG named *.webp
Fixes gohugoio#14288
1 parent 168bf17 commit 4085ee9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

‎resources/images/codec.go‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (d *Codec) EncodeTo(conf ImageConfig, w io.Writer, img image.Image) error {
132132
func (d *Codec) DecodeFormat(f Format, r io.Reader) (image.Image, error) {
133133
switch f {
134134
case JPEG, PNG:
135-
// TODO(bep) we reworked this decode/encode setup to get full WebP support in v0.153.0.
135+
// We reworked this decode/encode setup to get full WebP support in v0.153.0.
136136
// In the first take of that we used f to decide whether to call png.Decode or jpeg.Decode here,
137137
// but testing it on some sites, it seems that it's not uncommon to store JPEGs with PNG extensions and vice versa.
138138
// So, to reduce some noise in that release, we fallback to the standard library here,
@@ -153,7 +153,25 @@ func (d *Codec) DecodeFormat(f Format, r io.Reader) (image.Image, error) {
153153
case BMP:
154154
return bmp.Decode(r)
155155
case WEBP:
156-
return d.webp.Decode(r)
156+
img, err := d.webp.Decode(r)
157+
if err == nil {
158+
return img, nil
159+
}
160+
if rs, ok := r.(io.ReadSeeker); ok {
161+
// See issue 14288. Turns out it's not uncommon to e.g. name their PNG files with a WEBP extension.
162+
// With the old Go's webp decoder, this didn't fail (it looked for the file header),
163+
// but now some error has surfaced.
164+
// To reduce some noise, we try to reset and decode again using the standard library.
165+
_, err2 := rs.Seek(0, io.SeekStart)
166+
if err2 != nil {
167+
return nil, err
168+
}
169+
img, _, err2 = image.Decode(rs)
170+
if err2 == nil {
171+
return img, nil
172+
}
173+
}
174+
return nil, err
157175
default:
158176
return nil, errors.New("format not supported")
159177
}

‎tpl/images/images_integration_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,26 @@ Brightnes method: /qr_hu_d5f06fd7594d0594.png
165165
Brightnes func: /qr_hu_d5f06fd7594d0594.png
166166
`)
167167
}
168+
169+
func TestPNGNamedWebpIssue14288(t *testing.T) {
170+
t.Parallel()
171+
172+
files := `
173+
-- assets/qr.webp --
174+
sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
175+
-- layouts/home.html --
176+
{{ $img := resources.Get "qr.webp" }}
177+
images.Config: {{ (images.Config "assets/qr.webp").Width }}
178+
Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
179+
Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
180+
Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
181+
`
182+
183+
b := hugolib.Test(t, files)
184+
b.AssertFileContent("public/index.html", `
185+
images.Config: 222
186+
Resize to 100x100: 100
187+
Brightnes method: /qr_hu_d5f06fd7594d0594.webp
188+
Brightnes func: /qr_hu_d5f06fd7594d0594.webp
189+
`)
190+
}

0 commit comments

Comments
 (0)