High-performance WebP and AVIF image encoding/decoding library with bindings for Go, TypeScript/Node.js, Bun, and Deno.
This library provides a unified interface to libwebp and libavif, designed to match the behavior of official CLI tools (cwebp, dwebp, avifenc, avifdec) while offering convenient programmatic access across multiple languages.
- WebP & AVIF Support: Full encoding and decoding capabilities
- Multiple Language Bindings: Go, TypeScript/Node.js, Bun, Deno
- Binary Compatibility: Produces identical output to official CLI tools
- Cross-Platform: macOS (Intel/ARM), Linux (x64/ARM64), Windows
- Easy Installation: Automatic library download via package managers
- Zero External Dependencies: All required libraries bundled
Option 1: Automatic Download in Code
The library will automatically download pre-built binaries to a writable cache directory (~/.cache/libnextimage or $XDG_CACHE_HOME/libnextimage) when you call EnsureLibrary():
package main
import (
"log"
"github.com/ideamans/libnextimage/golang"
)
func main() {
// Ensure library is available (downloads if necessary)
if err := libnextimage.EnsureLibrary(); err != nil {
log.Fatal(err)
}
// Now you can use the library
// ...
}Option 2: Manual Pre-Installation
# Using standalone install tool to install to current directory
go run github.com/ideamans/libnextimage/golang/cmd/install@latest
# Or using install script to project directory
git clone https://github.com/ideamans/libnextimage.git
cd libnextimage
bash scripts/install.sh
# Install specific version
bash scripts/install.sh v0.1.0LIBNEXTIMAGE_CACHE_DIR: Custom cache directory for downloaded libraries (default:~/.cache/libnextimage)XDG_CACHE_HOME: Standard XDG cache directory (used ifLIBNEXTIMAGE_CACHE_DIRis not set)
If you prefer to build from source:
git clone --recursive https://github.com/ideamans/libnextimage.git
cd libnextimage
bash scripts/build-c-library.shThen use it in your Go project:
import "github.com/ideamans/libnextimage/golang"Install via npm - the native library will be automatically downloaded during installation:
npm install @ideamans/libnextimageThe package automatically downloads the appropriate native library for your platform (macOS, Linux, or Windows) from GitHub Releases.
Quick Start:
import { WebPEncoder, AVIFEncoder } from '@ideamans/libnextimage'
import { readFileSync, writeFileSync } from 'fs'
// Encode PNG to WebP
const pngData = readFileSync('input.png')
const webpEncoder = new WebPEncoder({ quality: 90 })
const webpData = webpEncoder.encode(pngData)
webpEncoder.close()
writeFileSync('output.webp', webpData)
// Encode to AVIF
const avifEncoder = new AVIFEncoder({ quality: 60, speed: 6 })
const avifData = avifEncoder.encode(pngData)
avifEncoder.close()
writeFileSync('output.avif', avifData)See typescript/README.md for detailed documentation.
Download pre-built binaries from GitHub Releases or use the install script:
# Clone repository
git clone https://github.com/ideamans/libnextimage.git
cd libnextimage
# Install for your platform (downloads from GitHub Releases)
bash scripts/install.sh
# Or install specific version
bash scripts/install.sh v0.4.0This installs:
lib/<platform>/libnextimage.a- Static library (for C/C++ and Go)include/*.h- Header files
For more details, see the Building from Source section.
import (
"os"
"github.com/ideamans/libnextimage/golang"
)
// Ensure library is available (downloads if necessary)
if err := libnextimage.EnsureLibrary(); err != nil {
panic(err)
}
// Encode PNG to WebP
data, _ := os.ReadFile("input.png")
opts := libnextimage.DefaultWebPEncodeOptions()
opts.Quality = 90
webpData, _ := libnextimage.WebPEncode(data, opts)
os.WriteFile("output.webp", webpData, 0644)
// Encode to AVIF
avifOpts := libnextimage.DefaultAVIFEncodeOptions()
avifOpts.Quality = 60
avifOpts.Speed = 6
avifData, _ := libnextimage.AVIFEncode(data, avifOpts)
os.WriteFile("output.avif", avifData, 0644)import { WebPEncoder, AVIFEncoder } from '@ideamans/libnextimage'
import { readFileSync, writeFileSync } from 'fs'
const imageData = readFileSync('input.png')
// WebP encoding
const webpEncoder = new WebPEncoder({ quality: 90 })
const webpData = webpEncoder.encode(imageData)
webpEncoder.close()
writeFileSync('output.webp', webpData)
// AVIF encoding
const avifEncoder = new AVIFEncoder({ quality: 60, speed: 6 })
const avifData = avifEncoder.encode(imageData)
avifEncoder.close()
writeFileSync('output.avif', avifData)#include "nextimage.h"
#include "webp.h"
NextImageWebPEncodeOptions opts;
nextimage_webp_default_encode_options(&opts);
opts.quality = 90;
NextImageBuffer input, output;
// ... load input data ...
NextImageStatus status = nextimage_webp_encode(
input.data, input.size, &opts, &output
);
if (status == NEXTIMAGE_STATUS_OK) {
// Use output.data, output.size
nextimage_buffer_free(&output);
}- macOS: Intel (x64), Apple Silicon (ARM64)
- Linux: x64, ARM64
- Windows: x64
Pre-built binaries are available for all platforms via GitHub Releases.
- CMake 3.15+
- C11 compiler (GCC, Clang, or MSVC)
- System libraries: libjpeg, libpng, libgif
brew install cmake jpeg libpng giflib
bash scripts/build-c-library.shsudo apt-get install cmake build-essential libjpeg-dev libpng-dev libgif-dev
bash scripts/build-c-library.shThe script will generate:
lib/<platform>/libnextimage.a- Combined static libraryinclude/*.h- Header files
cd golang
go test -vcd typescript
npm install
npm testAll tests verify binary-exact matching with official CLI tools.
This library produces byte-for-byte identical output to official CLI tools:
- ✅
cwebp/dwebp- WebP encoding/decoding - ✅
avifenc/avifdec- AVIF encoding/decoding - ✅
gif2webp/webp2gif- GIF conversion
This project is licensed under the BSD 3-Clause License.
- libwebp: BSD License
- libavif: BSD License
- libaom: BSD License
Contributions are welcome! Please ensure all tests pass before submitting pull requests.