@@ -132,7 +132,7 @@ func (d *Codec) EncodeTo(conf ImageConfig, w io.Writer, img image.Image) error {
132132func (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 }
0 commit comments