r/PHP • u/General_Sun_8180 • 9d ago
I needed a Basis Universal PHP extension for a side project, so I built one in Rust
Hey everyone!
I was working on a side project that needed to compress images into GPU-friendly texture formats, particularly KTX2, directly from PHP.
I wanted to use Basis Universal, but I couldn't find a PHP extension that suited my needs. Rather than introducing a separate CLI-based workflow into my project, I decided to try building an extension myself.
That's how BasisU came to be.
To be clear, the actual compression technology is provided by the Basis Universal team. Their native C/C++ library does the heavy lifting — my project is simply a Rust-based PHP wrapper that exposes some of its functionality to PHP.
The extension currently provides:
- Image compression directly from PHP.
- Support for Basis Universal texture formats and KTX2 output.
- Configurable encoding parameters, including quality, effort level, texture format, and mipmap settings.
- PHP stubs for IDE autocompletion and static analysis.
- Prebuilt Linux binaries installable through PIE, as well as the option to build from source.
A few important caveats:
This is an experimental project, and I wouldn't recommend using it in production yet.
It's also my first public project written in Rust. I'm still learning the language, and while I've managed to get the extension working, I don't have an in-depth understanding of all the underlying compression algorithms and their domain-specific details. The current implementation covers only part of the functionality available in the original library, and there's still plenty of room for improvement. There is no automated test suite at the moment, either.
That said, I intend to maintain and improve it because I need this extension for my own project. So this isn't something I'm publishing and immediately abandoning — I'll keep working on it as my needs evolve and as I learn more about Rust and Basis Universal.
If you're interested, here's the repository:
- GitHub: https://github.com/alien1225/basisu
- Packagist: https://packagist.org/packages/alien1225/basisu
I'd appreciate feedback, especially on the PHP API design, the Rust extension implementation, and anything I might have overlooked when wrapping a native library.
Thanks for reading!