{"record":{"id":"ff8c4c4c4eeec1ae","repo":"d2lang/d2","slug":"error-decoding-png-image-v","errorCode":null,"errorMessage":"error decoding PNG image: %v","messagePattern":"error decoding PNG image: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/pptx/pptx.go","lineNumber":107,"sourceCode":"func (p *Presentation) headerHeight() int {\n\tif p.includeNav {\n\t\treturn HEADER_HEIGHT\n\t}\n\treturn 0\n}\n\nfunc (p *Presentation) height() int {\n\treturn SLIDE_HEIGHT - p.headerHeight()\n}\n\nfunc (p *Presentation) aspectRatio() float64 {\n\treturn float64(IMAGE_WIDTH) / float64(p.height())\n}\n\nfunc (p *Presentation) AddSlide(pngContent []byte, titlePath []BoardTitle) (*Slide, error) {\n\tsrc, err := png.Decode(bytes.NewReader(pngContent))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error decoding PNG image: %v\", err)\n\t}\n\n\tvar width, height int\n\tsrcSize := src.Bounds().Size()\n\tsrcWidth, srcHeight := float64(srcSize.X), float64(srcSize.Y)\n\n\t// compute the size and position to fit the slide\n\t// if the image is wider than taller and its aspect ratio is, at least, the same as the available image space aspect ratio\n\t// then, set the image width to the available space and compute the height\n\t// ┌──────────────────────────────────────────────────┐   ─┬─\n\t// │  HEADER                                          │    │\n\t// ├──┬────────────────────────────────────────────┬──┤    │         ─┬─\n\t// │  │                                            │  │    │          │\n\t// │  │                                            │  │  SLIDE        │\n\t// │  │                                            │  │  HEIGHT       │\n\t// │  │                                            │  │    │        IMAGE\n\t// │  │                                            │  │    │        HEIGHT\n\t// │  │                                            │  │    │          │","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/lib/pptx/pptx.go#L89-L125","documentation":"Presentation.AddSlide decodes the provided PNG bytes with the png decoder before embedding them into a slide; if decoding fails (invalid, truncated, or non-PNG data), the error is wrapped with this message. The library requires valid PNG image content for each slide.","triggerScenarios":"Passing empty/nil byte slices, corrupted/truncated PNG files, or non-PNG data (JPEG, SVG text, HTML error pages) as pngContent to AddSlide — typically the output of a rendering pipeline that failed.","commonSituations":"Upstream renderer returned an error page captured as bytes; file read was truncated; wrong file extension fed in; an export step silently produced empty output.","solutions":["Validate the PNG before calling AddSlide (png.DecodeConfig or magic-bytes check \\x89PNG)","Check the upstream rendering step's error/status before using its bytes","Ensure the file was read completely (compare bytes read vs file size)","Confirm the source is actually PNG, not JPEG/SVG","Log the first bytes of pngContent to diagnose what was actually passed"],"exampleFix":"// before\nslide, err := pptx.AddSlide(pngBytes, titles)\n// after\nif len(pngBytes) < 8 || string(pngBytes[:8]) != \"\\x89PNG\\r\\n\\x1a\\n\" {\n    return fmt.Errorf(\"not a valid PNG (%d bytes)\", len(pngBytes))\n}\nslide, err := pptx.AddSlide(pngBytes, titles)","handlingStrategy":"validation","validationCode":"func isPNG(b []byte) bool {\n    return len(b) >= 8 && b[0]==0x89 && b[1]=='P' && b[2]=='N' && b[3]=='G' && b[4]==0x0d && b[5]==0x0a && b[6]==0x1a && b[7]==0x0a\n}\nif !isPNG(pngBytes) { return errors.New(\"invalid PNG input\") }","typeGuard":"func validPNG(b []byte) bool {\n    _, err := png.DecodeConfig(bytes.NewReader(b))\n    return err == nil\n}","tryCatchPattern":"slide, err := pres.AddSlide(pngBytes, titles)\nif err != nil {\n    if strings.Contains(err.Error(), \"error decoding PNG\") {\n        log.Printf(\"bad png (%d bytes, head=%x)\", len(pngBytes), pngBytes[:min(8,len(pngBytes))])\n        return reRenderImage()\n    }\n    return err\n}","preventionTips":["Check renderer errors before consuming its output bytes","Verify magic bytes/DecodeConfig on all image inputs","Ensure files are fully written/flushed before reading","Never feed JPEG/SVG bytes to AddSlide"],"tags":["go","png","image-decoding","validation","pptx"],"backgroundTag":"invalid-image-data","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}