|
| 1 | +# PHP binding for libvips |
| 2 | + |
| 3 | +[](https://travis-ci.org/libvips/php-vips) |
| 4 | + |
| 5 | +`php-vips` is a binding for [libvips](https://github.com/libvips/libvips) for |
| 6 | +PHP 7. |
| 7 | + |
| 8 | +libvips is fast and needs little memory. The |
| 9 | +[`vips-php-bench`](https://github.com/jcupitt/php-vips-bench) repository |
| 10 | +tests `php-vips` against `imagick` and `gd`. On that test, and on my laptop, |
| 11 | +`php-vips` is around four times faster than `imagick` and needs 10 times |
| 12 | +less memory. |
| 13 | + |
| 14 | +Programs that use libvips don't manipulate images directly, instead they |
| 15 | +create pipelines of image processing operations starting from a source |
| 16 | +image. When the pipe is connected to a destination, the whole pipeline |
| 17 | +executes at once and in parallel, streaming the image from source to |
| 18 | +destination in a set of small fragments. |
| 19 | + |
| 20 | +This module builds upon the `vips` PHP extension: |
| 21 | + |
| 22 | +https://github.com/libvips/php-vips-ext |
| 23 | + |
| 24 | +You'll need to install that first. It's tested on Linux and macOS --- |
| 25 | +Windows would need some work, but should be possible. |
| 26 | + |
| 27 | +See the README there, but briefly: |
| 28 | + |
| 29 | +1. [Install the libvips library and |
| 30 | + headers](https://libvips.github.io/libvips/install.html). It's in |
| 31 | + the linux package managers, homebrew and MacPorts, and there are Windows |
| 32 | + binaries on the vips website. For example, on Debian: |
| 33 | + |
| 34 | + ``` |
| 35 | + sudo apt-get install libvips-dev |
| 36 | + ``` |
| 37 | + |
| 38 | + Or macOS: |
| 39 | + |
| 40 | + ``` |
| 41 | + brew install vips |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Install the binary PHP extension: |
| 45 | + |
| 46 | + ``` |
| 47 | + pecl install vips |
| 48 | + ``` |
| 49 | + |
| 50 | + You may need to add `extension=vips.so` or equivalent to `php.ini`, see the |
| 51 | + output of pecl. |
| 52 | + |
| 53 | +3. Add vips to your `composer.json`: |
| 54 | + |
| 55 | + ``` |
| 56 | + "require": { |
| 57 | + "jcupitt/vips" : "1.0.5" |
| 58 | + } |
| 59 | + ``` |
| 60 | + |
| 61 | +### Example |
| 62 | + |
| 63 | +```php |
| 64 | +#!/usr/bin/env php |
| 65 | +<?php |
| 66 | +require __DIR__ . '/vendor/autoload.php'; |
| 67 | +use Jcupitt\Vips; |
| 68 | + |
| 69 | +// fast thumbnail generator |
| 70 | +$image = Vips\Image::thumbnail('somefile.jpg', 128); |
| 71 | +$image->writeToFile('tiny.jpg'); |
| 72 | + |
| 73 | +// load an image, get fields, process, save |
| 74 | +$image = Vips\Image::newFromFile($argv[1]); |
| 75 | +echo "width = $image->width\n"; |
| 76 | +$image = $image->invert(); |
| 77 | +$image->writeToFile($argv[2]); |
| 78 | +``` |
| 79 | + |
| 80 | +Run with: |
| 81 | + |
| 82 | +``` |
| 83 | +$ composer install |
| 84 | +$ ./try1.php ~/pics/k2.jpg x.tif |
| 85 | +``` |
| 86 | + |
| 87 | +See `examples/`. We have a [complete set of formatted API |
| 88 | +docs](https://libvips.github.io/php-vips/docs/classes/Jcupitt-Vips-Image.html). |
| 89 | + |
| 90 | +### Introduction to the API |
| 91 | + |
| 92 | +Almost all methods return a new image as the result, so you can chain them. |
| 93 | +For example: |
| 94 | + |
| 95 | +```php |
| 96 | +$new_image = $image->more(12)->ifthenelse(255, $image); |
| 97 | +``` |
| 98 | + |
| 99 | +will make a mask of pixels greater than 12, then use the mask to set pixels to |
| 100 | +either 255 or the original image. |
| 101 | + |
| 102 | +Note that libvips operators always make new images, they don't modify existing |
| 103 | +images, so after the line above, `$image` is unchanged. |
| 104 | + |
| 105 | +You use long, double, array and image as parameters. For example: |
| 106 | + |
| 107 | +```php |
| 108 | +$image = $image->add(2); |
| 109 | +``` |
| 110 | + |
| 111 | +to add two to every band element, or: |
| 112 | + |
| 113 | +```php |
| 114 | +$image = $image->add([1, 2, 3]); |
| 115 | +``` |
| 116 | + |
| 117 | +to add 1 to the first band, 2 to the second and 3 to the third. Or: |
| 118 | + |
| 119 | +```php |
| 120 | +$image = $image->add($image2); |
| 121 | +``` |
| 122 | + |
| 123 | +to add two images. Or: |
| 124 | + |
| 125 | +```php |
| 126 | +$image = $image->add([[1, 2, 3], [4, 5, 6]]); |
| 127 | +``` |
| 128 | + |
| 129 | +To make a 2 x 3 image from the array, then add that image to the original. |
| 130 | + |
| 131 | +Almost all methods can take an extra final argument: an array of options. |
| 132 | +For example: |
| 133 | + |
| 134 | +```php |
| 135 | +$image->writeToFile("fred.jpg", ["Q" => 90]); |
| 136 | +``` |
| 137 | + |
| 138 | +`php-vips` comes [with full API docs](https://libvips.github.io/php-vips/docs/classes/Jcupitt.Vips.Image.html). To regenerate these from your sources, type: |
| 139 | + |
| 140 | +``` |
| 141 | +$ vendor/bin/phpdoc |
| 142 | +``` |
| 143 | + |
| 144 | +And look in `docs/`. |
| 145 | + |
| 146 | +There are around 300 operations in the library, see the vips docs for an |
| 147 | +introduction: |
| 148 | + |
| 149 | +https://libvips.github.io/libvips/API/current |
| 150 | + |
| 151 | +### How it works |
| 152 | + |
| 153 | +The `vips` extension defines a simple but ugly way to call any libvips |
| 154 | +operation from PHP. It uses libvips' own introspection facilities |
| 155 | +and does not depend on anything else (so no gobject-introspection, |
| 156 | +for example). It's a fairly short 1,600 lines of C. |
| 157 | + |
| 158 | +This module is a PHP layer over the ugly `vips` extension that |
| 159 | +tries to make a nice interface for programmers. It uses `__call()` and |
| 160 | +`__get()` to make all libvips operations appear as methods, and all |
| 161 | +libvips properties as properties of the PHP `Vips\Image` class. |
| 162 | + |
| 163 | +### Test and install |
| 164 | + |
| 165 | +``` |
| 166 | +$ composer install |
| 167 | +$ composer test |
| 168 | +$ vendor/bin/phpdoc |
| 169 | +``` |
| 170 | + |
| 171 | +### Regenerate auto docs |
| 172 | + |
| 173 | +``` |
| 174 | +$ cd src |
| 175 | +$ ../examples/generate_phpdoc.py |
| 176 | +``` |
| 177 | + |
0 commit comments