{"record":{"id":"946a1fba26e51597","repo":"siyuan-note/siyuan","slug":"encode-image-failed-s","errorCode":null,"errorMessage":"encode image failed: %s","messagePattern":"encode image failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":783,"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":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/util/openai.go#L765-L801","documentation":"When the source image is PNG or WebP and needed resizing/re-encoding, PrepareModelImage re-encodes the decoded image with Go's image/png encoder; an encoder failure is wrapped as \"encode image failed: %s\" (kernel/util/openai.go:783). PNG encoding of a valid in-memory image essentially only fails on buffer write errors, so this is very rare and usually signals an underlying I/O or memory problem.","triggerScenarios":"PrepareModelImage is called with a PNG or WebP image that requires resizing (larger than maxEdge) or a re-encode (e.g. WebP needing re-encode to PNG), and image/png's Encode returns an error while writing to the bytes.Buffer.","commonSituations":"Extremely large images causing memory pressure while encoding; a corrupted decoded image state from the imaging library; out-of-memory conditions on constrained environments during Lanczos resize + encode.","solutions":["Retry with the original image after confirming it decodes correctly; the error is rarely caused by the input format.","Check available memory — resizing very large images via imaging.Fit with Lanczos plus PNG encoding is memory-heavy; reduce maxEdge or image dimensions.","Inspect the wrapped error text; if it indicates memory/write failure, increase system resources or pre-shrink the image externally.","As a workaround, convert the source to JPEG beforehand so the code path uses jpeg.Encode instead."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"prepared, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\nif err != nil && strings.HasPrefix(err.Error(), \"encode image failed\") {\n    // retry once; otherwise fall back to sending a pre-shrunk JPEG\n}","preventionTips":["Bound input image dimensions before calling so resize+encode stays within memory limits","Monitor available memory in constrained environments when processing large PNG/WebP images","Fall back to a plain JPEG path if PNG re-encoding repeatedly fails"],"tags":["image-processing","png-encode","rare"],"backgroundTag":"json-marshal-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}