A Go library for converting traditional web image formats (JPEG, PNG, GIF) to next-generation formats (WebP, AVIF) following best practices.
- Convert JPEG/PNG/GIF images to WebP format
- Convert JPEG/PNG images to AVIF format
- Automatic image optimization with size reduction checks
- Preserve important metadata (ICC profiles)
- Support for animated GIF to WebP conversion
- Configurable quality settings
- Thread-safe concurrent conversions
go get github.com/ideamans/go-next-gen-imageThis library requires libvips to be installed on your system:
Ubuntu/Debian:
sudo apt-get install libvips-devmacOS:
brew install vipsWindows:
choco install vipspackage main
import (
"log"
nextgenimage "github.com/ideamans/go-next-gen-image"
)
func main() {
// Create converter with default settings
converter := nextgenimage.NewConverter(nextgenimage.ConverterConfig{})
// Convert JPEG to WebP
err := converter.ToWebP("input.jpg", "output.webp")
if err != nil {
log.Fatal(err)
}
// Convert PNG to AVIF
err = converter.ToAVIF("input.png", "output.avif")
if err != nil {
log.Fatal(err)
}
}config := nextgenimage.ConverterConfig{
JPEGToWebP: struct {
Quality int
}{
Quality: 85, // Default: 80
},
PNGToWebP: struct {
TryNearLossless bool
}{
TryNearLossless: true, // Default: false
},
JPEGToAVIF: struct {
CQ int
}{
CQ: 20, // Default: 25
},
}
converter := nextgenimage.NewConverter(config)- Lossy compression
- Configurable quality (default: 80)
- Auto-rotation based on EXIF orientation
- Removes all metadata (EXIF, XMP, ICC)
- Lossless compression by default
- Optional near-lossless mode for better compression
- Removes all metadata (EXIF, XMP, ICC)
- Alpha channel support
- Lossless frame conversion
- Animation preservation (timing, loops)
- All frames converted to WebP animation
- Lossy compression with CQ (Constant Quality) mode
- Configurable CQ value (default: 25, lower = better quality)
- Auto-rotation based on EXIF orientation
- Removes all metadata (EXIF, XMP, ICC)
- Lossless compression
- Removes all metadata (EXIF, XMP, ICC)
- Alpha channel support
- Not supported (returns FormatError)
The library distinguishes between data-related errors and system errors:
err := converter.ToWebP("input.jpg", "output.webp")
if err != nil {
var formatErr *nextgenimage.FormatError
if errors.As(err, &formatErr) {
// Handle format-specific error (e.g., unsupported format, size increase)
log.Printf("Format error: %v", err)
} else {
// Handle system error (e.g., file not found, permission denied)
log.Printf("System error: %v", err)
}
}- The library uses libvips for efficient image processing
- Supports concurrent conversions (thread-safe)
- Automatically checks output size and returns FormatError if the converted image is larger than the original
Run all tests:
go test ./...Run tests with coverage:
go test -race -coverprofile=coverage.out ./...
go tool cover -html=coverage.outMIT License - see LICENSE file for details
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate and ensure all tests pass before submitting a PR.