]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/images: Don't trust the file extension when decoding JPEG and PNG images
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 18 Dec 2025 11:00:20 +0000 (12:00 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 18 Dec 2025 18:36:48 +0000 (19:36 +0100)
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.

resources/images/codec.go

index 6d84a682f3e53dd428412ff33a3b4e1abcd467ba..2bd93b579c3f548748a522efe2a5c239fa927070 100644 (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 {