Writing WebP files in PHP, without GD or libwebp
I’ve published PicoWebP, a WebP encoder written in PHP that works without GD, Imagick, libwebp or cwebp. It preserves transparency and colour profiles, supports resumable conversion, and trades speed for the ability to run on hosts without native image tools.
I’ve published PicoWebP, a WebP encoder written in PHP.
The interesting part is where the conversion happens. PHP reads the supported source image and writes the WebP bitstream itself. You can convert a PNG or baseline JPEG without GD, Imagick, libwebp or cwebp installed.
This is for the awkward hosting setup where PHP works, but installing an image extension or running a binary isn’t an option. The package needs PHP 8.1+ and zlib, so there are still requirements. They just don’t include a native image encoder.
Getting a file that opens was only part of the work. Transparent PNGs need to remain transparent. Colour profiles need to carry through. Unsupported JPEG variants need a useful error. An image that is already WebP should be skipped, including when its filename claims otherwise.
PicoWebP preserves the alpha channel losslessly and carries embedded ICC profiles into the output. The image’s colour data is still encoded lossily. Progressive JPEGs currently need GD for decoding.
And yes, doing this in PHP is slow.
On the benchmark machine, the default encoder took about 25 seconds for a roughly one-megapixel image and more than six minutes for a 12-megapixel image, before adding source decoding. These are background jobs.
There is also a resumable path: an application can process part of an image, save the encoder state and continue later. That makes longer conversions easier to schedule within hosting limits. It does not make the encoding cheaper.
The reason to use PicoWebP is straightforward: it gives a restricted PHP host another way to produce WebP files. A native encoder remains the better choice when one is available.
The package is available on Packagist, with the source on GitHub, under the MIT licence. I’ve put the overview on the PicoWebP project page.