Skip to content

Commit fc7516c

Browse files
authored
Switch to php ffi (#133)
Rewrite for php-ffi
1 parent b23917a commit fc7516c

21 files changed

+2656
-654
lines changed

CHANGELOG.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
# Changelog
2+
23
All notable changes to `:vips` will be documented in this file.
34

5+
## 2.0.0 - 2022-1-20
6+
7+
Rewritten to use PHP FFI to call into the libvips library rather than a binary
8+
extension. This means php-vips now requires php 7.4 or later.
9+
10+
### Added
11+
- `Interpolate` class
12+
13+
### Deprecated
14+
- Nothing
15+
16+
### Fixed
17+
- Nothing
18+
19+
### Remove
20+
- Nothing
21+
22+
### Security
23+
- Nothing
24+
425
### 1.0.9 - 2021-11-20
526

627
### Added
@@ -18,7 +39,7 @@ All notable changes to `:vips` will be documented in this file.
1839
### Security
1940
- Nothing
2041

21-
### 1.0.8 - 2020-08-29
42+
## 1.0.8 - 2020-08-29
2243

2344
### Added
2445
- allow type names as type params to Image::setType() -- fixes issue with GType
@@ -36,7 +57,7 @@ All notable changes to `:vips` will be documented in this file.
3657
### Security
3758
- Nothing
3859

39-
### 1.0.7 - 2020-08-28
60+
## 1.0.7 - 2020-08-28
4061

4162
### Added
4263
- use nullable types and void return type where possible

README.md

+29-53
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[![Build Status](https://travis-ci.org/libvips/php-vips.svg?branch=master)](https://travis-ci.org/libvips/php-vips)
44

5-
`php-vips` is a binding for [libvips](https://github.com/libvips/libvips) for
6-
PHP 7.
5+
`php-vips` is a binding for [libvips](https://github.com/libvips/libvips) 8.7
6+
and later for PHP 7.4 and later.
77

88
libvips is fast and needs little memory. The
99
[`vips-php-bench`](https://github.com/jcupitt/php-vips-bench) repository
@@ -17,54 +17,30 @@ image. When the pipe is connected to a destination, the whole pipeline
1717
executes at once and in parallel, streaming the image from source to
1818
destination in a set of small fragments.
1919

20-
This module builds upon the `vips` PHP extension:
20+
### Install
2121

22-
https://github.com/libvips/php-vips-ext
22+
You need to [install the libvips
23+
library](https://libvips.github.io/libvips/install.html). It's in the linux
24+
package managers, homebrew and MacPorts, and there are Windows binaries on
25+
the vips website. For example, on Debian:
2326

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. You'll need a PHP development environment
45-
for this, since it will download and build the sources for the extension.
46-
For example, on Debian:
47-
48-
```
49-
sudo apt-get install php-pear
50-
```
51-
52-
Then to download and build the extension it's:
27+
```
28+
sudo apt-get install libvips-dev
29+
```
5330

54-
```
55-
pecl install vips
56-
```
31+
Or macOS:
5732

58-
You may need to add `extension=vips.so` or equivalent to `php.ini`, see the
59-
output of pecl.
33+
```
34+
brew install vips
35+
```
6036

61-
3. Add vips to your `composer.json`:
37+
Then add vips to your `composer.json`:
6238

63-
```
64-
"require": {
65-
"jcupitt/vips" : "1.0.7"
66-
}
67-
```
39+
```
40+
"require": {
41+
"jcupitt/vips" : "2.0.0"
42+
}
43+
```
6844

6945
### Example
7046

@@ -156,17 +132,17 @@ introduction:
156132

157133
https://libvips.github.io/libvips/API/current
158134

159-
### How it works
135+
### TODO after merge
136+
137+
- Support preloading, see https://www.php.net/manual/en/class.ffi.php
138+
139+
- Rewrite the enum and doc generator in php.
140+
141+
- Add source/target API
160142

161-
The `vips` extension defines a simple but ugly way to call any libvips
162-
operation from PHP. It uses libvips' own introspection facilities
163-
and does not depend on anything else (so no gobject-introspection,
164-
for example). It's a fairly short 1,600 lines of C.
143+
- Add progress callbacks etc.
165144

166-
This module is a PHP layer over the ugly `vips` extension that
167-
tries to make a nice interface for programmers. It uses `__call()` and
168-
`__get()` to make all libvips operations appear as methods, and all
169-
libvips properties as properties of the PHP `Vips\Image` class.
145+
- Add mutable.
170146

171147
### Test and install
172148

File renamed without changes.

composer.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=7.2",
21-
"ext-vips": ">=0.1.2",
20+
"php": ">=7.4",
21+
"ext-ffi": "*",
2222
"psr/log": "^1.1.3|^2.0|^3.0"
2323
},
2424
"require-dev": {
25-
"php-parallel-lint/php-parallel-lint": "^1.2",
26-
"phpdocumentor/phpdocumentor": "3.0.0",
27-
"phpunit/phpunit": "^9.3",
28-
"squizlabs/php_codesniffer": "^3.5"
25+
"php-parallel-lint/php-parallel-lint": "^1.3",
26+
"phpdocumentor/shim": "^3.3",
27+
"phpunit/phpunit": "^9.5",
28+
"squizlabs/php_codesniffer": "^3.6"
2929
},
3030
"autoload": {
3131
"psr-4": {
@@ -39,7 +39,7 @@
3939
},
4040
"extra": {
4141
"branch-alias": {
42-
"dev-master": "1.0.x-dev"
42+
"dev-master": "2.0.x-dev"
4343
}
4444
},
4545
"scripts": {

examples/composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"jcupitt/vips": "2.0.0"
4+
}
5+
}

examples/watermark-image.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__ . '/vendor/autoload.php';
5+
use Jcupitt\Vips;
6+
7+
Vips\Config::setLogger(new Vips\DebugLogger());
8+
9+
if (count($argv) != 4) {
10+
echo("usage: ./watermark.php input-image output-image watermark-image\n");
11+
exit(1);
12+
}
13+
14+
// we can stream the main image
15+
$image = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);
16+
17+
// we'll read the watermark image many times, so we need random access for this
18+
$watermark = Vips\Image::newFromFile($argv[3]);
19+
20+
// the watermark image needs to have an alpha channel
21+
if (!$watermark->hasAlpha() || $watermark->bands != 4) {
22+
echo("watermark image is not RGBA\n");
23+
exit(1);
24+
}
25+
26+
// make the watermark semi-transparent
27+
$watermark = $watermark->multiply([1, 1, 1, 0.3])->cast("uchar");
28+
29+
// repeat the watermark to the size of the image
30+
$watermark = $watermark->replicate(
31+
1 + $image->width / $watermark->width,
32+
1 + $image->height / $watermark->height
33+
);
34+
$watermark = $watermark->crop(0, 0, $image->width, $image->height);
35+
36+
// composite the watermark over the main image
37+
$image = $image->composite2($watermark, 'over');
38+
39+
$image->writeToFile($argv[2]);
40+
41+
$image = null;
42+
$watermark = null;
43+
44+
Vips\Config::shutDown();

examples/watermark-text.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require __DIR__ . '/vendor/autoload.php';
55
use Jcupitt\Vips;
66

7+
#Vips\Config::setLogger(new Vips\DebugLogger());
8+
79
if (count($argv) != 4) {
810
echo("usage: ./watermark-text.php input output \"some text\"\n");
911
exit(1);
@@ -30,7 +32,7 @@
3032
$foreground = [255, 255, 255, 50];
3133
$background = [0, 0, 255, 50];
3234

33-
// and a 10-pixel marghin
35+
// and a 10-pixel margin
3436
$margin = 10;
3537

3638
$overlay = $text_mask->ifthenelse($foreground, $background, [

src/ArgumentFlags.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* PHP version 7
5+
*
6+
* LICENSE:
7+
*
8+
* Copyright (c) 2016 John Cupitt
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining
11+
* a copy of this software and associated documentation files (the
12+
* "Software"), to deal in the Software without restriction, including
13+
* without limitation the rights to use, copy, modify, merge, publish,
14+
* distribute, sublicense, and/or sell copies of the Software, and to
15+
* permit persons to whom the Software is furnished to do so, subject to
16+
* the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be
19+
* included in all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28+
*
29+
* @category Images
30+
* @package Jcupitt\Vips
31+
* @author John Cupitt <jcupitt@gmail.com>
32+
* @copyright 2016 John Cupitt
33+
* @license https://opensource.org/licenses/MIT MIT
34+
* @link https://github.com/jcupitt/php-vips
35+
*/
36+
37+
namespace Jcupitt\Vips;
38+
39+
/**
40+
* The ArgumentFlags enum.
41+
* @category Images
42+
* @package Jcupitt\Vips
43+
* @author John Cupitt <jcupitt@gmail.com>
44+
* @copyright 2016 John Cupitt
45+
* @license https://opensource.org/licenses/MIT MIT
46+
* @link https://github.com/jcupitt/php-vips
47+
*/
48+
abstract class ArgumentFlags
49+
{
50+
const REQUIRED = 1;
51+
const CONSTRUCT = 2;
52+
const SET_ONCE = 4;
53+
const SET_ALWAYS = 8;
54+
const INPUT = 16;
55+
const OUTPUT = 32;
56+
const DEPRECATED = 64;
57+
const MODIFY = 128;
58+
59+
const NAMES = [
60+
"REQUIRED" => self::REQUIRED,
61+
"CONSTRUCT" => self::CONSTRUCT,
62+
"SET_ONCE" => self::SET_ONCE,
63+
"SET_ALWAYS" => self::SET_ALWAYS,
64+
"INPUT" => self::INPUT,
65+
"OUTPUT" => self::OUTPUT,
66+
"DEPRECATED" => self::DEPRECATED,
67+
"MODIFY" => self::MODIFY,
68+
];
69+
}

0 commit comments

Comments
 (0)