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
This commit is contained in:
2026-01-08 13:12:34 -08:00
parent 32f9166ece
commit ca4446e10a

View File

@@ -133,6 +133,7 @@ func (p *ImageProcessor) SupportedOutputFormats() []ImageFormat {
FormatPNG, FormatPNG,
FormatGIF, FormatGIF,
FormatWebP, FormatWebP,
FormatAVIF,
} }
} }
@@ -218,6 +219,9 @@ func (p *ImageProcessor) resize(img image.Image, width, height int, fit FitMode)
const defaultQuality = 85 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. // encode encodes an image to the specified format.
func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int) ([]byte, error) { func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int) ([]byte, error) {
if quality <= 0 { if quality <= 0 {
@@ -256,7 +260,14 @@ func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int
} }
case FormatAVIF: 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: default:
return nil, fmt.Errorf("unsupported output format: %s", format) return nil, fmt.Errorf("unsupported output format: %s", format)