Image Pipeline

Last updated


This page walks through GET /c/img/*path: deciding whether to transform, the libvips processing order, and the streamed response.

Routing Decisions


GetFromPath checks these in order and responds on the first match:
OrderConditionBehavior
1Path ends in .pdfStream the original as application/pdf
2Path ends in .svgStream the original as image/svg+xml
3o=1Infer MIME from the extension (GetMimeType) and stream the original
4Local cache file existsSet Content-Type from t and stream the cache file
5OtherwiseTransform with libvips

Every branch first sets 7-day Cache-Control and Expires headers. In branches 1–3 a missing original returns 404 with body File not found.

Transform Steps


The transform runs in a goroutine under a 30-second timeout:
StepCallNotes
1os.MkdirAllCreate the cache file's directory
2os.ReadFile → vips.NewImageFromBufferRead and decode the original
3GetNewSize → Resize(scale, KernelLanczos3)Scale proportionally with Lanczos3 when the size changes (rules in Sizing Rules)
4GaussianBlur(GetBlur(b))Gaussian blur, sigma clamped to 0–100
5Linear([B], [0])Brightness: multiply every band by B; a non-numeric B fails the transform
6ExportJpeg / ExportPng / ExportAvif / ExportWebpExport by t; PNG ignores quality
7os.WriteFileWrite the local cache file

Any failed step returns the 404 placeholder SVG instead (dark variant for d=1 or d=dark) with Cache-Control: no-cache; utils.HandleGetError serves it through c.File, so the HTTP status is 200.

Streamed Output


streamImage (in-memory buffer) and streamFile (original file) both respond with Transfer-Encoding: chunked, flushing after each buffer fills:
File sizeBuffer
≤ 50 KiB2 KiB
≤ 500 KiB4 KiB
≤ 2 MiB8 KiB
Larger16 KiB

streamFile also sends Content-Length; when the ResponseWriter does not implement http.Flusher, it falls back to a single c.File.
中文