apache/answer · error

decode webp image error: %v

Error message

decode webp image error: %v

What it means

webpImageCheck wraps errors from webp.Decode at pkg/checker/file_type.go:145. Thrown when the WebP header is valid but full pixel decoding fails — corrupt VP8/VP8L bitstream, truncation mid-frame, or unsupported extended-format features (e.g. some animation/alpha combinations in x/image/webp).

Source

Thrown at pkg/checker/file_type.go:145

	}
	return nil
}

func webpImageConfigCheck(file io.Reader, _ string, maxImageMegapixel int) error {
	config, err := webp.DecodeConfig(file)
	if err != nil {
		return fmt.Errorf("decode webp image config error: %v", err)
	}
	if imageSizeTooLarge(config, maxImageMegapixel) {
		return fmt.Errorf("image size too large")
	}
	return nil
}

func webpImageCheck(file io.Reader, _ string, _ int) error {
	_, err := webp.Decode(file)
	if err != nil {
		return fmt.Errorf("decode webp image error: %v", err)
	}
	return nil
}

func imageSizeTooLarge(config image.Config, maxImageMegapixel int) bool {
	return config.Width*config.Height > maxImageMegapixel
}

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Upgrade golang.org/x/image to the latest release for broader WebP (animation/alpha) support
  2. Re-encode the file with ffmpeg/cwebp to a plain lossy or lossless WebP
  3. Check for upload truncation (Content-Length vs received bytes, proxy buffering)
  4. Convert to PNG/JPEG server-side as a fallback when WebP full decode fails

Example fix

// before: pin old x/image
require golang.org/x/image v0.0.0-2019...
// after
go get golang.org/x/image@latest // supports newer WebP features
Defensive patterns

Strategy: try-catch

Validate before calling

head := make([]byte, 12)
io.ReadFull(f, head)
if string(head[0:4]) != "RIFF" || string(head[8:12]) != "WEBP" { return errors.New("not webp") }

Type guard

func isWebPFullDecodeErr(err error) bool { return err != nil && strings.HasPrefix(err.Error(), "decode webp image error") }

Try / catch

if _, err := webp.Decode(f); err != nil {
	// fall back to another decoder or re-encode via external tool
	return fmt.Errorf("webp full decode failed: %w", err)
}

Prevention

When it happens

Trigger: Second-stage full decode of a .webp file: body corruption after a valid header, truncated frame data, or animated/extended WebP that x/image/webp cannot fully decode.

Common situations: Animated WebP from modern browsers/encoders rejected by the older x/image/webp library; interrupted uploads; WebP files damaged by non-binary-safe transfer.

Related errors


AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05). Data as JSON: /api/errors/ba68cc7bf9ac3526. Report an issue: GitHub.