{"record":{"id":"df43eb163c411d5f","repo":"siyuan-note/siyuan","slug":"encode-image-failed","errorCode":null,"errorMessage":"encode image failed: ","messagePattern":"encode image failed: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":667,"sourceCode":"\tbounds := decoded.Bounds()\n\tneedsResize := maxEdge > 0 && (bounds.Dx() > maxEdge || bounds.Dy() > maxEdge)\n\tif !needsResize && bounds.Dx() == config.Width && bounds.Dy() == config.Height && mimeType != \"image/gif\" {\n\t\treturn PreparedImage{\n\t\t\tData:       data,\n\t\t\tMIMEType:   mimeType,\n\t\t\tWidth:      bounds.Dx(),\n\t\t\tHeight:     bounds.Dy(),\n\t\t\tSourceSize: len(data),\n\t\t}, nil\n\t}\n\tif needsResize {\n\t\tdecoded = imaging.Fit(decoded, maxEdge, maxEdge, imaging.Lanczos)\n\t}\n\tbounds = decoded.Bounds()\n\tif mimeType == \"image/png\" || mimeType == \"image/webp\" {\n\t\tvar output bytes.Buffer\n\t\tif err = png.Encode(&output, decoded); err != nil {\n\t\t\treturn PreparedImage{}, errors.New(\"encode image failed: \" + err.Error())\n\t\t}\n\t\tif maxBytes <= 0 || output.Len() <= maxBytes {\n\t\t\treturn PreparedImage{\n\t\t\t\tData:       output.Bytes(),\n\t\t\t\tMIMEType:   \"image/png\",\n\t\t\t\tWidth:      bounds.Dx(),\n\t\t\t\tHeight:     bounds.Dy(),\n\t\t\t\tSourceSize: len(data),\n\t\t\t}, nil\n\t\t}\n\t}\n\tvar output bytes.Buffer\n\tif err = jpeg.Encode(&output, decoded, &jpeg.Options{Quality: 92}); err != nil {\n\t\treturn PreparedImage{}, errors.New(\"encode image failed: \" + err.Error())\n\t}\n\tif maxBytes > 0 && output.Len() > maxBytes {\n\t\treturn PreparedImage{}, fmt.Errorf(\"prepared image exceeds size limit: %d bytes\", maxBytes)\n\t}","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/openai.go#L649-L685","documentation":"For PNG/WebP sources (or any case that reaches the PNG encode path) PrepareModelImage re-encodes via png.Encode after resize. Encoder failure is very rare and indicates a degenerate decoded image (e.g. zero-area frame from a malformed GIF) or memory pressure.","triggerScenarios":"Pathological GIF frame decoded to a degenerate image; memory pressure during encode; an unusual decoded image the PNG encoder cannot handle.","commonSituations":"Malformed animated GIF whose frame is zero-area; very large frame under memory pressure.","solutions":["Re-save the source as a clean PNG/JPEG.","For a GIF, extract a single representative frame as PNG instead of feeding the whole GIF.","Reduce input resolution to relieve memory."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := util.PrepareModelImage(data, maxB, maxP, maxE); err != nil {\n    log.Warnf(\"drop unencodable image: %s\", err)\n    continue\n}","preventionTips":["Use clean PNG/JPEG sources","Extract a single GIF frame for vision rather than the whole GIF"],"tags":["ai","multimodal","image","go","encoder"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}