resources/images: Don't trust the file extension when decoding JPEG and PNG images

Which is it behaved before we added the extended WebP support.

Testing on some sites shows that it's not uncommon to store JPEGs with PNG extensions and vice versa.
This an error situation that needs to be reported, but let us push that to a future Hugo version to reduce the noise in this one.
This commit is contained in:
Bjørn Erik Pedersen
2025-12-18 12:00:20 +01:00
parent 65a76664e3
commit 65d43e1d0d
+8 -4
View File
@@ -131,10 +131,14 @@ func (d *Codec) EncodeTo(conf ImageConfig, w io.Writer, img image.Image) error {
func (d *Codec) DecodeFormat(f Format, r io.Reader) (image.Image, error) {
switch f {
case JPEG:
return jpeg.Decode(r)
case PNG:
return png.Decode(r)
case JPEG, PNG:
// TODO(bep) we reworked this decode/encode setup to get full WebP support in v0.153.0.
// In the first take of that we used f to decide whether to call png.Decode or jpeg.Decode here,
// but testing it on some sites, it seems that it's not uncommon to store JPEGs with PNG extensions and vice versa.
// So, to reduce some noise in that release, we fallback to the standard library here,
// which will read the magic bytes and decode accordingly.
img, _, err := image.Decode(r)
return img, err
case GIF:
g, err := gif.DecodeAll(r)
if err != nil {