2017 © Pedro Peláez
 

library vips

image

jcupitt/vips

  • Monday, July 30, 2018
  • by jcupitt
  • Repository
  • 14 Watchers
  • 164 Stars
  • 53,312 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 8 Forks
  • 4 Open issues
  • 11 Versions
  • 7 % Grown

The README.md

PHP binding for libvips

CI, (*1)

php-vips is a binding for libvips 8.7 and later that runs on PHP 7.4 and later., (*2)

libvips is fast and needs little memory. The vips-php-bench repository tests php-vips against imagick and gd. On that test, and on my laptop, php-vips is around four times faster than imagick and needs 10 times less memory., (*3)

Programs that use libvips don't manipulate images directly, instead they create pipelines of image processing operations starting from a source image. When the pipe is connected to a destination, the whole pipeline executes at once and in parallel, streaming the image from source to destination in a set of small fragments., (*4)

Install

You need to install the libvips library. It's in the linux package managers, homebrew and MacPorts, and there are Windows binaries on the vips website. For example, on Debian:, (*5)

sudo apt-get install --no-install-recommends libvips42

(--no-install-recommends stops Debian installing a lot of extra packages), (*6)

Or macOS:, (*7)

brew install vips

You'll need to enable FFI in your PHP, then add vips to your composer.json:, (*8)

"require": {
    "jcupitt/vips" : "2.4.0"
}

php-vips does not yet support preloading, so you need to enable FFI globally. This has some security implications, since anyone who can run php on your server can use it to call any native library they have access to., (*9)

Of course if attackers are running their own PHP code on your webserver you are probably already toast, unfortunately., (*10)

Finally, on php 8.3 and later you need to disable stack overflow tests. php-vips executes FFI callbacks off the main thread and this confuses those checks, at least in php 8.3.0., (*11)

Add:, (*12)

zend.max_allowed_stack_size=-1

To your php.ini., (*13)

Example

#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

// handy for Windows
Vips\FFI::addLibraryPath("C:/vips-dev-8.16/bin");

// check libvips version
echo 'libvips version: ' . Vips\Config::version() . PHP_EOL;

// fast thumbnail generator
$image = Vips\Image::thumbnail('somefile.jpg', 128);
$image->writeToFile('tiny.jpg');

// load an image, get fields, process, save
$image = Vips\Image::newFromFile($argv[1]);
echo "width = $image->width\n";
$image = $image->invert();
$image->writeToFile($argv[2]);

Run with:, (*14)

$ composer install
$ ./try1.php ~/pics/k2.jpg x.tif

See examples/. We have a complete set of formatted API docs., (*15)

How it works

php-vips uses php-ffi to call directly into the libvips binary. It introspects the library binary and presents the methods it finds as members of the Image class., (*16)

This means that the API you see depends on the version of libvips that php-vips finds at runtime, and not on php-vips. php-vips documentation assumes you are using the latest stable version of the libvips library., (*17)

The previous php-vips version that relied on a binary extension and not on php-ffi is still available and supported in the 1.x branch., (*18)

Introduction to the API

Almost all methods return a new image as the result, so you can chain them. For example:, (*19)

$new_image = $image->more(12)->ifthenelse(255, $image);

will make a mask of pixels greater than 12, then use the mask to set pixels to either 255 or the original image., (*20)

Note that libvips operators always make new images, they don't modify existing images, so after the line above, $image is unchanged., (*21)

You can use long, double, array and image as parameters. For example:, (*22)

$image = $image->add(2);

to add two to every band element, or:, (*23)

$image = $image->add([1, 2, 3]);

to add 1 to the first band, 2 to the second and 3 to the third. Or:, (*24)

$image = $image->add($image2);

to add two images. Or:, (*25)

$image = $image->add([[1, 2, 3], [4, 5, 6]]);

To make a 3 x 2 image from the array, then add that image to the original., (*26)

Almost all methods can take an extra final argument: an array of options. For example:, (*27)

$image->writeToFile("fred.jpg", ["Q" => 90]);

php-vips comes with API docs. To regenerate these from your sources, type:, (*28)

$ vendor/bin/phpdoc

And look in docs/., (*29)

Unfortunatly, due to php-doc limitations, these do not list every option to every operation. For a full API description you need to see the main libvips documentation:, (*30)

https://libvips.org/API/current, (*31)

Test and install

$ composer install
$ composer test
$ vendor/bin/phpdoc

Regenerate auto docs

$ cd src
$ ../examples/generate_phpdoc.py

The Versions

30/07 2018

dev-add-header-get

dev-add-header-get

  Sources   Download

10/06 2018

dev-master

9999999-dev https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

08/12 2017

dev-add-throws-decls

dev-add-throws-decls https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

08/12 2017

dev-vips-8.6

dev-vips-8.6 https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

06/06 2017

dev-add-array-access

dev-add-array-access https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

30/04 2017

v1.0.2

1.0.2.0 https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

17/04 2017

v1.0.1

1.0.1.0 https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

22/03 2017

dev-use-travis-containers

dev-use-travis-containers https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

04/11 2016

v1.0.0

1.0.0.0 https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

04/11 2016

dev-dev

dev-dev https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

 

The Development Requires

image processing libvips

19/10 2016

v0.1.2

0.1.2.0 https://github.com/jcupitt/php-vips

A high-level interface to the libvips image processing library.

  Sources   Download

MIT

The Requires

  • php >=7.0.11
  • ext-vips >=0.1.0

 

The Development Requires

image processing libvips