{"record":{"id":"946a1fba26e51597","repo":"siyuan-note/siyuan","slug":"encode-image-failed-s","errorCode":null,"errorMessage":"encode image failed: %s","messagePattern":"encode image failed: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":807,"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":789,"sourceCodeEnd":825,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai.go#L789-L825","documentation":"Thrown at kernel/util/openai.go:807 when re-encoding a decoded (and possibly resized) PNG or WebP image back to PNG via png.Encode fails. This is rare because PNG encoding of an in-memory NRGBA image seldom fails, but it can happen under memory exhaustion or with zero-size images.","triggerScenarios":"After resizing with imaging.Fit, encoding the resulting image to PNG fails (png.Encode returns a non-nil error) — typically out-of-memory on very large canvases or a degenerate zero-dimension image.","commonSituations":"Very large images combined with low available memory; images that decode to 0×0 bounds; resource-constrained environments where the encoder cannot allocate buffers.","solutions":["Free memory / reduce concurrent image processing, or use a smaller source image","Skip processing and send the original bytes if they are already within the byte limit","Ensure the decoded image has non-zero bounds before encoding","Retry the operation once memory pressure subsides"],"exampleFix":"// before: unconditional re-encode on any resize\nif needsResize { decoded = imaging.Fit(decoded, maxEdge, maxEdge, imaging.Lanczos) }\nout, err := reencodePNGorFail(decoded)\n// after: reuse original bytes when no resize is actually needed\nif !needsResize {\n    return useOriginalBytes(data)\n}\ndecoded = imaging.Fit(decoded, maxEdge, maxEdge, imaging.Lanczos)\nout, err := reencodePNGorFail(decoded)","handlingStrategy":"try-catch","validationCode":"if decoded.Bounds().Dx() < 1 || decoded.Bounds().Dy() < 1 {\n    return errors.New(\"cannot encode empty image\")\n}","typeGuard":null,"tryCatchPattern":"if err := encodePNG(&output, decoded); err != nil {\n    if isOOM(err) {\n        // fall back to original bytes or a smaller resize\n        return useOriginalOrSmaller(data)\n    }\n    return err\n}","preventionTips":["Reuse original bytes whenever no resize is needed instead of re-encoding","Bound source image sizes before decoding to limit encoder memory","Ensure images have non-zero bounds before encoding","Process images sequentially in memory-constrained environments"],"tags":["image","encoding","png","memory","ai"],"backgroundTag":"file-write-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}