Add WebP encoding support

Uses github.com/gen2brain/webp - a CGO-free library that uses WASM via
wazero runtime for encoding. WebP decoding was already supported.

- Add gen2brain/webp dependency for encoding
- Implement WebP encoding in processor.go
- Add FormatWebP to SupportedOutputFormats
- Re-enable WebP option in generator form dropdown
- Mark WebP encoding as complete in TODO.md
This commit is contained in:
2026-01-08 11:55:45 -08:00
parent 0c9eb35bd2
commit 70d55977c0
5 changed files with 21 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import (
"io"
"github.com/disintegration/imaging"
gowebp "github.com/gen2brain/webp"
"golang.org/x/image/webp"
)
@@ -122,6 +123,7 @@ func (p *ImageProcessor) SupportedOutputFormats() []ImageFormat {
FormatJPEG,
FormatPNG,
FormatGIF,
FormatWebP,
}
}
@@ -227,7 +229,14 @@ func (p *ImageProcessor) encode(img image.Image, format ImageFormat, quality int
}
case FormatWebP:
return nil, fmt.Errorf("%w: WebP encoding not supported", ErrUnsupportedOutputFormat)
options := gowebp.Options{
Lossless: false,
Quality: quality,
}
err := gowebp.Encode(&buf, img, options)
if err != nil {
return nil, err
}
case FormatAVIF:
return nil, fmt.Errorf("%w: AVIF encoding not supported", ErrUnsupportedOutputFormat)

View File

@@ -108,6 +108,7 @@
<option value="orig" {{if eq .FormFormat "orig"}}selected{{end}}>Original</option>
<option value="jpeg" {{if eq .FormFormat "jpeg"}}selected{{end}}>JPEG</option>
<option value="png" {{if eq .FormFormat "png"}}selected{{end}}>PNG</option>
<option value="webp" {{if eq .FormFormat "webp"}}selected{{end}}>WebP</option>
<option value="gif" {{if eq .FormFormat "gif"}}selected{{end}}>GIF</option>
</select>
</div>