Skip to content

Commit 65d43e1

Browse files
committed
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.
1 parent 65a7666 commit 65d43e1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

‎resources/images/codec.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ func (d *Codec) EncodeTo(conf ImageConfig, w io.Writer, img image.Image) error {
131131

132132
func (d *Codec) DecodeFormat(f Format, r io.Reader) (image.Image, error) {
133133
switch f {
134-
case JPEG:
135-
return jpeg.Decode(r)
136-
case PNG:
137-
return png.Decode(r)
134+
case JPEG, PNG:
135+
// TODO(bep) we reworked this decode/encode setup to get full WebP support in v0.153.0.
136+
// In the first take of that we used f to decide whether to call png.Decode or jpeg.Decode here,
137+
// but testing it on some sites, it seems that it's not uncommon to store JPEGs with PNG extensions and vice versa.
138+
// So, to reduce some noise in that release, we fallback to the standard library here,
139+
// which will read the magic bytes and decode accordingly.
140+
img, _, err := image.Decode(r)
141+
return img, err
138142
case GIF:
139143
g, err := gif.DecodeAll(r)
140144
if err != nil {

0 commit comments

Comments
 (0)