libvips/php-vips
php binding for libvips
What It Is
php-vips is a PHP extension that gives you access to the libvips image processing library. libvips is known for being very fast and memory‑efficient. According to the project's benchmarks, php-vips runs about four times faster than the imagick extension and uses ten times less memory.
How It Works
Instead of loading an entire image into memory and editing it pixel by pixel, libvips works with pipelines of operations. You chain processing steps together, and the whole pipeline is executed at once, streamed through the image in small fragments. php-vips uses PHP’s FFI (Foreign Function Interface) to call directly into the libvips binary. It inspects the library at runtime and turns its available methods into PHP methods on an Image class. That means the exact set of functions you can use depends on the version of libvips installed on your server.
Installing
- Install libvips – available via your system’s package manager (e.g.
apt install libvips42on Debian,brew install vipson macOS, or Windows binaries from the libvips website). - Enable FFI in PHP – you must turn on FFI in your
php.ini. This has security implications, because any PHP code running on your server could then call any native library it has access to. (The README notes that if someone is already running code on your server, they can probably breach it anyway.) - Add the Composer dependency – include
"jcupitt/vips": "2.4.0"in yourcomposer.jsonand runcomposer install. - For PHP 8.3+ – you also need to disable stack overflow tests by adding
zend.max_allowed_stack_size=-1tophp.ini, because FFI callbacks run off the main thread.
Basic Usage
The README shows a typical example:
- Create a fast thumbnail:
$image = Vips\Image::thumbnail('somefile.jpg', 128); $image->writeToFile('tiny.jpg'); - Load an image, read its width, invert it, and save:
$image = Vips\Image::newFromFile($argv[1]); echo "width = $image->width\n"; $image = $image->invert(); $image->writeToFile($argv[2]);
Chaining and Parameters
Almost every method returns a new image, so you can chain calls. For example:
$new_image = $image->more(12)->ifthenelse(255, $image);
This creates a mask of pixels greater than 12, then uses that mask to set pixels to 255 or the original image. The original $image is never modified; each step produces a separate result.
You can pass numbers, arrays, or other images as parameters. For instance:
$image->add(2)– add 2 to every band.$image->add([1, 2, 3])– add 1 to the first band, 2 to the second, 3 to the third.$image->add($otherImage)– add two images.$image->add([[1,2,3],[4,5,6]])– construct an image from the array and add it.
You can also pass an options array as the final argument. For example, to set JPEG quality:
$image->writeToFile("fred.jpg", ["Q" => 90]);
Documentation and Tests
The project includes formatted API documentation that can be regenerated with vendor/bin/phpdoc. For a full list of all options to every operation, you need to consult the main libvips documentation.
To run the test suite, use composer test after installing dependencies.
Older Version
A previous version of php-vips that used a binary extension (instead of FFI) is still available in the 1.x branch.
Related
- Project
- Project
- Project
- Project