From ca4446e10a4aa82bc92a3c0868b81b31a0af4c9c Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 8 Jan 2026 13:12:34 -0800 Subject: [PATCH] Implement AVIF encoding support - Add AVIF case to encode() using gen2brain/avif - Add AVIF to SupportedOutputFormats() - Use default encoder speed of 6 for balance of speed/quality --- internal/imgcache/processor.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/imgcache/processor.go b/internal/imgcache/processor.go index c101c23..332002c 100644 --- a/internal/imgcache/processor.go +++ b/internal/imgcache/processor.go @@ -133,6 +133,7 @@ func (p *ImageProcessor) SupportedOutputFormats() []ImageFormat { FormatPNG, FormatGIF, FormatWebP, + FormatAVIF, } } @@ -218,6 +219,9 @@ func (p *ImageProcessor) resize(img image.Image, width, height int, fit FitMode) const defaultQuality = 85 +// avifEncoderSpeed is the default AVIF encoder speed (0-10, higher = faster, lower quality). +const avifEncoderSpeed = 6 + // encode encodes an image to the specified format. func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int) ([]byte, error) { if quality <= 0 { @@ -256,7 +260,14 @@ func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int } case FormatAVIF: - return nil, fmt.Errorf("%w: AVIF encoding not supported", ErrUnsupportedOutputFormat) + options := goavif.Options{ + Quality: quality, + Speed: avifEncoderSpeed, + } + err := goavif.Encode(&buf, img, options) + if err != nil { + return nil, err + } default: return nil, fmt.Errorf("unsupported output format: %s", format)