Documentation
¶
Overview ¶
Package thumbnail renders small preview images for files: a downscaled version of a raster image, or a synthetic card for text, code, and everything else.
Render never fails on bad input. An undecodable image, an oversized one, or a format the package cannot read all degrade to a typed card, so every file gets a stable visual and callers need no fallback path of their own.
It is pure Go and depends only on the standard library and golang.org/x/image: stdlib decoders for PNG/JPEG/GIF, x/image/webp for WebP, x/image/draw for high-quality downscaling, and x/image/font with the embedded Go fonts for the cards. There is no FFmpeg, Chromium, or ImageMagick dependency and no system-font requirement, so it runs unchanged in a minimal container image.
Output is image/jpeg (quality 82) for opaque photographic thumbnails and image/png for cards and images that may carry transparency.
Index ¶
Constants ¶
const ( RendererImage = "image" RendererDocument = "document" RendererFallback = "fallback" )
Renderer names reported in Result.Renderer for diagnostics.
const DefaultMaxSourcePixels = 24 * 1000 * 1000 // 24 MP
DefaultMaxSourcePixels bounds the decoded area of a source image. Larger images fall back to a typed card rather than decoding (a decode allocates roughly width*height*4 bytes).
Variables ¶
This section is empty.
Functions ¶
func IsImageMime ¶
IsImageMime reports whether the MIME is a raster format this package can decode and downscale. Exported for callers that decide how many source bytes to read before rendering.
func IsTextMime ¶
IsTextMime reports whether the MIME/name should render as a text document card. Exported for the same byte-budget decision as IsImageMime.
Types ¶
type Request ¶
type Request struct {
// MimeType is the source MIME type (e.g. image/png, text/markdown).
MimeType string
// Name is the display name of the source; used for card labels and
// extension detection.
Name string
// Source is the source bytes. For images this should be the whole file
// (the caller caps how much it reads); for text a leading slice is enough,
// since only the first few lines are drawn.
Source []byte
// Width and Height are the target thumbnail box.
Width int
Height int
// MaxSourcePixels overrides DefaultMaxSourcePixels when > 0.
MaxSourcePixels int
}
Request is the input to Render.
type Result ¶
type Result struct {
Bytes []byte
MimeType string // image/jpeg or image/png
Width int
Height int
Renderer string // RendererImage | RendererDocument | RendererFallback
Note string // fallback reason / diagnostic, empty on the happy path
}
Result is a rendered thumbnail.
func Render ¶
Render produces a thumbnail for the request. It never returns an error for an unsupported or malformed source: image failures and non-image types fall through to a deterministic typed card, so every input gets a stable visual. A non-nil error indicates an encoding fault, not bad input.
Width and Height default to 320x200 when unset.