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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user