|
| 1 | +# Low-level PHP binding for libvips |
| 2 | + |
| 3 | +This extension lets you use the libvips image processing library from PHP. It is |
| 4 | +intentionally very low-level. Modules such as `vips` build a nice API on |
| 5 | +top of this extension. |
| 6 | + |
| 7 | +libvips is fast and it can work without needing to have the |
| 8 | +entire image loaded into memory. Programs that use libvips don't |
| 9 | +manipulate images directly, instead they create pipelines of image processing |
| 10 | +operations building on a source image. When the end of the pipe is connected |
| 11 | +to a destination, the whole pipline executes at once, streaming the image |
| 12 | +in parallel from source to destination in a set of small fragments. |
| 13 | + |
| 14 | +See the [benchmarks at the official libvips |
| 15 | +website](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use). |
| 16 | +There's a handy blog post explaining [how libvips opens |
| 17 | +files](http://libvips.blogspot.co.uk/2012/06/how-libvips-opens-file.html) |
| 18 | +which gives some more background. |
| 19 | + |
| 20 | +### Example |
| 21 | + |
| 22 | +```php |
| 23 | +#!/usr/bin/env php |
| 24 | +<?php |
| 25 | +if (!extension_loaded("vips")) { |
| 26 | + dl('vips.' . PHP_SHLIB_SUFFIX); |
| 27 | +} |
| 28 | + |
| 29 | +$x = vips_image_new_from_file($argv[1])["out"]; |
| 30 | +$x = vips_call("invert", $x)["out"]; |
| 31 | +vips_image_write_to_file($x, $argv[2]); |
| 32 | +?> |
| 33 | +``` |
| 34 | + |
| 35 | +Almost all operations return an array of result values. Usually there is a |
| 36 | +single result called `"out"`. |
| 37 | + |
| 38 | +Use `vips_call()` to call any operation in the vips library. There are around |
| 39 | +around 300 operations available, see the vips docs for an |
| 40 | +introduction: |
| 41 | + |
| 42 | +http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/ |
| 43 | + |
| 44 | +Arguments can be long, double, image, array of long, array of double or array |
| 45 | +of image. The final argument to `vips_call()` is an array of operation options. |
| 46 | + |
| 47 | +### Preparation |
| 48 | + |
| 49 | +PHP is normally built for speed and is missing a lot of debugging support you |
| 50 | +need for extension development. For testing and dev, build your own php. |
| 51 | +I used 7.0.10 and configured with: |
| 52 | + |
| 53 | +``` |
| 54 | +$ ./configure --prefix=/home/john/vips --enable-debug --enable-maintainer-zts \ |
| 55 | + --enable-cgi --enable-cli --with-readline --with-openssl |
| 56 | +``` |
| 57 | + |
| 58 | +You'll need libvips 8.0 or later, including all the headers for development. |
| 59 | +On linux, install with your package manager. |
| 60 | +On OS X, install with `brew` or MacPorts. For Windows, download a zip from |
| 61 | +the libvips |
| 62 | +website, or build your own. |
| 63 | + |
| 64 | +### Regenerate build system |
| 65 | + |
| 66 | +Run: |
| 67 | + |
| 68 | +``` |
| 69 | +$ phpize |
| 70 | +``` |
| 71 | + |
| 72 | +To scan `config.m4` and your php install and regenerate the build system. |
| 73 | + |
| 74 | +### Configuring |
| 75 | + |
| 76 | +Run |
| 77 | + |
| 78 | +``` |
| 79 | +$ ./configure |
| 80 | +``` |
| 81 | + |
| 82 | +Check the output carefully for errors, and obviously check that it found your |
| 83 | +libvips. |
| 84 | + |
| 85 | +### Installing |
| 86 | + |
| 87 | +Run: |
| 88 | + |
| 89 | + |
| 90 | +``` |
| 91 | +$ make |
| 92 | +``` |
| 93 | + |
| 94 | +To build the module to the `modules/` directory in this repository. |
| 95 | + |
| 96 | +Don't post php-vips test results to php.net! Stop this with: |
| 97 | + |
| 98 | + |
| 99 | +``` |
| 100 | +$ export NO_INTERACTION=1 |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +Test with: |
| 105 | + |
| 106 | + |
| 107 | +``` |
| 108 | +$ make test |
| 109 | +``` |
| 110 | + |
| 111 | +Finally, install to your php extensions area with: |
| 112 | + |
| 113 | +``` |
| 114 | +$ make install |
| 115 | +``` |
| 116 | + |
| 117 | +### Using |
| 118 | + |
| 119 | +Try: |
| 120 | + |
| 121 | +```php |
| 122 | +#!/usr/bin/env php |
| 123 | +<?php |
| 124 | +if (!extension_loaded("vips")) { |
| 125 | + dl('vips.' . PHP_SHLIB_SUFFIX); |
| 126 | +} |
| 127 | + |
| 128 | +$x = vips_image_new_from_file($argv[1])["out"]; |
| 129 | +vips_image_write_to_file($x, $argv[2]); |
| 130 | +?> |
| 131 | +``` |
| 132 | + |
| 133 | +And run with: |
| 134 | + |
| 135 | +``` |
| 136 | +$ ./try1.php ~/pics/k2.jpg x.tif |
| 137 | +``` |
| 138 | + |
| 139 | +See `examples/`. |
| 140 | + |
| 141 | +### Links |
| 142 | + |
| 143 | +http://php.net/manual/en/internals2.php |
| 144 | + |
| 145 | +https://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/ |
| 146 | + |
| 147 | +https://devzone.zend.com/317/extension-writing-part-ii-parameters-arrays-and-zvals/ |
| 148 | + |
| 149 | +https://devzone.zend.com/446/extension-writing-part-iii-resources/ |
| 150 | + |
| 151 | +### Documentation |
| 152 | + |
| 153 | +``` |
| 154 | +$ pear channel-discover pear.phpdoc.org |
| 155 | +$ pear install phpdoc/phpDocumentor |
| 156 | +$ phpdoc |
| 157 | +``` |
| 158 | + |
0 commit comments