Media API

Piko's media service transforms images and videos. It resizes, re-encodes, builds responsive variant sets, generates low-quality placeholders, transcodes video, and emits HLS or DASH streams. The service is provider-agnostic. The default image backend is libvips and the default video backend is ffmpeg via astiav. For task recipes see the assets how-to. Source of truth: wdk/media/facade.go.

Service

FunctionReturns
media.NewService(defaultTransformerName string) ImageServiceConstructs a new image service.
media.GetDefaultService() (ImageService, error)Returns the bootstrap-configured service.
media.GetImageDimensions(ctx, reader)Width and height of an image.

Transform builder

func NewTransformBuilder(service ImageService, input io.Reader) *TransformBuilder
func NewTransformBuilderFromDefault(input io.Reader) (*TransformBuilder, error)

TransformBuilder is a fluent wrapper around ImageService.TransformStream. Each setter mutates the underlying TransformationSpec and returns the builder. Do executes the spec (returning a result), and DoToWriter streams directly to an io.Writer.

Sizing

MethodEffect
.Width(px int)Set target width. 0 preserves aspect ratio from the height.
.Height(px int)Set target height. 0 preserves aspect ratio from the width.
.Size(width, height int)Set both axes at once.
.MaxWidth(px int)Set width and clear height (scale to fit width).
.MaxHeight(px int)Set height and clear width (scale to fit height).
.AspectRatio(ratio string)Force an aspect ratio ("16:9", "4:3", "1:1").
.WithoutEnlargement()Prevent scaling beyond the source's natural size.

Fit

MethodEquivalent to
.Fit(mode FitMode)Set the fit mode explicitly.
.Cover()Fit(FitCover). Fills the box, cropping excess.
.Contain()Fit(FitContain). Letterboxes to keep the whole image.
.Fill()Fit(FitFill). Stretches to exact dimensions.
.Inside()Fit(FitInside). Never exceeds bounds.
.Outside()Fit(FitOutside). At least covers bounds.

Output and modifiers

MethodEffect
.Format(format string)Output format ("jpeg", "png", "webp", "avif", "gif").
.Quality(q int)Compression quality 1-100.
.Background(hex string)Letterbox or transparency-fill colour ("#RRGGBB").
.Provider(name string)Use a specific provider instead of the service default.
.WithModifier(key, value string)Add a provider-specific modifier.
.Blur(sigma float64)Convenience: sets the blur modifier.
.Greyscale()Convenience: sets the greyscale modifier.

Variants and specs

MethodEffect
.WithPredefinedVariants(map[string]TransformationSpec)Register named variant lookups for UseVariant.
.UseVariant(name string)Replace the current spec with a registered variant.
.FromSpec(spec TransformationSpec)Replace the current spec with the supplied one.
.Spec() TransformationSpecReturn the current spec without executing.

Terminals

MethodEffect
.Do(ctx context.Context)Execute and return *TransformedImageResult.
.DoToWriter(ctx context.Context, w io.Writer) errorExecute and stream the output to w.

Responsive and placeholder generation

ImageService calls produce responsive variants and low-quality placeholders, not TransformBuilder methods.

service.GenerateResponsiveVariants(ctx, input, baseSpec) // reads baseSpec.Responsive *ResponsiveSpec
service.GeneratePlaceholder(ctx, input, baseSpec)        // reads baseSpec.Placeholder *PlaceholderSpec

TransformationSpec carries optional Responsive *ResponsiveSpec and Placeholder *PlaceholderSpec fields that those methods consume.

Config and spec builders

Image() *ImageConfigBuilder
Variant() *VariantBuilder
GetPredefinedVariants() map[string]TransformationSpec
GetVariantSpec(name string) (TransformationSpec, bool)

Types

Images

TypePurpose
ImageServiceManages image providers.
ImageTransformerPortInterface a backend implements.
ImageServiceConfigService defaults.
TransformationSpecNamed transformation.
FitModeCrop or letterbox behaviour.
ResponsiveSpecGenerates multi-width variants.
ResponsiveVariantOne variant in a responsive set.
PlaceholderSpecLow-quality image placeholder.
ImageConfigPer-request settings.
ImageConfigBuilder, VariantBuilder, TransformBuilderFluent builders.
TransformedImageResultOutput blob plus metadata.

Videos

TypePurpose
VideoServiceManages video providers.
VideoTranscoderPortInterface a backend implements.
StreamingTranscoderPortStreaming variant.
VideoServiceConfigService defaults.
TranscodeSpecOutput format and encoding.
ThumbnailSpecFrame extraction spec.
VideoCapabilitiesDeclares provider support.
HLSSpec, HLSResult, HLSVariant, HLSSegmentHLS streaming.
DASHSpec, DASHResultDASH streaming.
TranscodeResultOutput metadata.

Fit modes

ConstantMeaning
FitCoverFill the box, cropping as needed.
FitContainLetterbox: the whole image is visible.
FitFillStretch to the exact box without preserving aspect.
FitInsidePreserve aspect, do not exceed bounds.
FitOutsidePreserve aspect, cover bounds.

Errors

ErrUnsupportedCodec, ErrUnsupportedFormat, ErrInvalidResolution, ErrInvalidBitrate, ErrInvalidFramerate, ErrDurationExceedsLimit, ErrFileSizeExceedsLimit, ErrResolutionExceedsLimit, ErrTranscodingFailed, ErrInvalidStream, ErrContextCancelled, ErrTimeout, ErrResourceExhausted, ErrInvalidHLSSpec, ErrSegmentationFailed.

Providers

Sub-packageBackend
image_provider_vipslibvips (high performance).
image_provider_imagingPure-Go imaging library.
video_provider_astiavffmpeg via astiav.

Bootstrap options

OptionPurpose
piko.WithImageProvider(name, provider)Registers an image provider.
piko.WithDefaultImageProvider(name)Marks default.
piko.WithImage(cfg)Registers per-request image defaults.
piko.WithImageService(service)Registers a fully configured service.
piko.WithVideoProvider(name, provider)Registers a video provider.
piko.WithDefaultVideoProvider(name)Marks default.
piko.WithVideoService(service)Registers a fully configured service.

See also