xenforo / libvips
A native PHP extension for libvips: bounded, safe image processing with a small immutable API.
Package info
github.com/xenforo-ltd/php-libvips
Type:php-ext
Ext name:ext-libvips
pkg:composer/xenforo/libvips
Requires
- php: >=8.1
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A native PHP extension for libvips, built for servers that process images
uploaded by people they don't trust. It gives you a small, immutable image API in the Vips\ namespace,
with a fixed set of formats, size limits checked before decoding, and a time limit on every evaluation.
Alpha. 0.1.0 is the first public release. The API and settings may still change before 1.0.
<?php use Vips\Crop; use Vips\Format; use Vips\Image; use Vips\LimitExceededException; try { $image = Image::fromFile('/path/to/upload.jpg'); $image ->thumbnail(300, 300, crop: Crop::Attention, autoOrient: true) ->writeToFile('/path/to/thumb.webp', Format::Webp, ['quality' => 80]); } catch (LimitExceededException $e) { // Larger than the configured limits (libvips.max_pixels, ...). }
Install
You need PHP 8.1 or later, libvips 8.12 or later with its development files, and a C toolchain. Then, with PIE:
pie install xenforo/libvips
docs/install.md lists the packages for each platform and every php.ini setting.
What it does
- Loads JPEG, PNG, GIF and WebP by default. AVIF and JPEG XL input can be turned on; all six formats can be saved. TIFF, PDF, SVG, HEIC, RAW and ImageMagick are always refused. AVIF files are checked before libvips sees them: every image in the file must really be AV1, whatever its label says.
- Checks limits from the header, before any pixels are decoded: width, height, pixels, frames, total
pixels across all frames, and input size. Administrators set them in
php.ini; scripts can only tighten them. - Stops slow work. Every evaluation has a wall-clock limit (60 seconds by default).
- Keeps libvips away from paths and option strings. PHP opens and reads files (so
open_basedirapplies), every method has a fixed, typed signature, and no PHP code runs on libvips' threads. - Strips metadata when saving (EXIF, XMP, GPS, comments; the colour profile is kept) unless you ask to keep it, and replaces files atomically.
- Handles animation. Animated GIF and WebP keep their frames through thumbnails, crops, rotations and saves.
How it compares
- Imagick and GD: libvips uses much less memory and is usually faster. In our tests peak memory was around a fifth of Imagick's, and wall time about a third. GD keeps only the first frame of an animation. See docs/performance.md.
- jcupitt/vips (FFI): the same speed on the same libvips,
with all of libvips available. It needs
ffi.enable, which lets any PHP code call any native library, so it doesn't fit shared hosting. This extension trades breadth for a narrow, checked API. - The old PECL
vipsextension: deprecated by its author. This extension is a different code base, with a different API and module name.
Documentation
- Installing and configuring
- API reference
- Security model, and how to report a vulnerability
- Performance
- Changelog
- Contributing
Licence
MIT. See LICENSE.