Skip to content

Switch to php ffi #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f4ad0fc
start an ffi experiment
jcupitt Dec 7, 2021
f38fb69
fix syntax errors
jcupitt Jan 8, 2022
2dccec1
do the rest of introspection
jcupitt Jan 8, 2022
7284eee
oop typo
jcupitt Jan 8, 2022
4ae4cb5
add the rest of call operation
jcupitt Jan 12, 2022
d6a9f65
start movinf ffi into php-vips
jcupitt Jan 13, 2022
3fecf05
start pasting in startup code
jcupitt Jan 15, 2022
5503608
more pasting in
jcupitt Jan 19, 2022
2ebad55
more hacking
jcupitt Jan 28, 2022
a2e3698
update README
jcupitt Jan 29, 2022
6f4a0bd
new_from_file etc.
jcupitt Jan 29, 2022
e495a75
more fixes
jcupitt Jan 29, 2022
fea3f81
try to get it to init again
jcupitt Jan 29, 2022
3951af8
make init into a singleton class
jcupitt Jan 29, 2022
096cc4e
move the singleton to a static member
jcupitt Jan 30, 2022
d06e82f
fixed up init
jcupitt Jan 30, 2022
0990d62
hack hack hack
jcupitt Feb 4, 2022
151b3f2
introspect working
jcupitt Feb 5, 2022
36710a2
almost there
jcupitt Feb 5, 2022
f220389
working ... gvalue missing cases next
jcupitt Feb 6, 2022
a460710
start fleshing out get/set
jcupitt Feb 6, 2022
bf56100
small bugfixes
jcupitt Feb 7, 2022
f8be77f
now executes a test program!
jcupitt Feb 13, 2022
da5cb08
runs the watermark-image example successfully
jcupitt Feb 14, 2022
8c18365
fix optional args on newFromFile
jcupitt Feb 14, 2022
3f6516d
add the rest of GValue get()
jcupitt Feb 14, 2022
a0df283
can't get callStatic working from methods
jcupitt Feb 14, 2022
4b3e2b6
watermark-test works!
jcupitt Feb 14, 2022
389c7d4
start fixing tests
jcupitt Feb 14, 2022
b4b9d6b
Allowed for psr/log 2.0 and 3.0 to be installed as well
boris-glumpler Feb 3, 2022
0fbb9e7
start adding constant imagazation
jcupitt Feb 15, 2022
82f3b65
down to 13 failures
jcupitt Feb 15, 2022
70538f0
9 errors left
jcupitt Feb 15, 2022
5c21316
nail a coupl;e more small errors
jcupitt Feb 16, 2022
548e1e4
down to 3 test suite fails
jcupitt Feb 16, 2022
fefe62f
down to two fails
jcupitt Feb 16, 2022
7bd9c99
two errors left
jcupitt Feb 16, 2022
be90a10
all tests pass
jcupitt Feb 17, 2022
401c80e
fix phpcs warnings
jcupitt Feb 17, 2022
6869216
start soak testing
jcupitt Feb 17, 2022
7073b6e
fix a segv
jcupitt Feb 28, 2022
692dbc5
revise string handling
jcupitt Feb 28, 2022
05327a9
revise startup
jcupitt Mar 1, 2022
8537cba
more init into config
jcupitt Mar 1, 2022
44ad854
start fixing up tests again
jcupitt Mar 1, 2022
9c0bcce
all tests pass cleanly
jcupitt Mar 1, 2022
01e4e9f
use new_from_memory_copy for now
jcupitt Mar 1, 2022
249d7fa
revise docs
jcupitt Mar 1, 2022
ed25bc9
final checks
jcupitt Mar 1, 2022
162d0ce
Merge branch 'master' into switch-to-php-ffi
jcupitt Mar 1, 2022
998ca6d
add findLoad and findLoadBuffer
jcupitt Mar 6, 2022
156a772
add writeToArray()
jcupitt Mar 7, 2022
ff75115
Update src/Config.php
jcupitt Mar 7, 2022
2b0cfc8
incorporate review comments
jcupitt Mar 7, 2022
4640d83
Merge branch 'switch-to-php-ffi' of github.com:libvips/php-vips into …
jcupitt Mar 7, 2022
47d9fbb
fix testVipsWriteToBuffer with options
jcupitt Mar 7, 2022
387f4c1
try to improve shared library finding
jcupitt Mar 7, 2022
009411d
make debugLog less chatty
jcupitt Mar 7, 2022
8b30c9f
revise library name generation again
jcupitt Mar 7, 2022
c202836
add __clone()
jcupitt Mar 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# Changelog

All notable changes to `:vips` will be documented in this file.

## 2.0.0 - 2022-1-20

Rewritten to use PHP FFI to call into the libvips library rather than a binary
extension. This means php-vips now requires php 7.4 or later.

### Added
- `Interpolate` class

### Deprecated
- Nothing

### Fixed
- Nothing

### Remove
- Nothing

### Security
- Nothing

### 1.0.9 - 2021-11-20

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

### 1.0.8 - 2020-08-29
## 1.0.8 - 2020-08-29

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

### 1.0.7 - 2020-08-28
## 1.0.7 - 2020-08-28

### Added
- use nullable types and void return type where possible
Expand Down
82 changes: 29 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

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

libvips is fast and needs little memory. The
[`vips-php-bench`](https://github.com/jcupitt/php-vips-bench) repository
Expand All @@ -17,54 +17,30 @@ 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.

This module builds upon the `vips` PHP extension:
### Install

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

You'll need to install that first. It's tested on Linux and macOS ---
Windows would need some work, but should be possible.

See the README there, but briefly:

1. [Install the libvips library and
headers](https://libvips.github.io/libvips/install.html). It's in
the linux package managers, homebrew and MacPorts, and there are Windows
binaries on the vips website. For example, on Debian:

```
sudo apt-get install libvips-dev
```

Or macOS:

```
brew install vips
```

2. Install the binary PHP extension. You'll need a PHP development environment
for this, since it will download and build the sources for the extension.
For example, on Debian:

```
sudo apt-get install php-pear
```

Then to download and build the extension it's:
```
sudo apt-get install libvips-dev
```

```
pecl install vips
```
Or macOS:

You may need to add `extension=vips.so` or equivalent to `php.ini`, see the
output of pecl.
```
brew install vips
```

3. Add vips to your `composer.json`:
Then add vips to your `composer.json`:

```
"require": {
"jcupitt/vips" : "1.0.7"
}
```
```
"require": {
"jcupitt/vips" : "2.0.0"
}
```

### Example

Expand Down Expand Up @@ -156,17 +132,17 @@ introduction:

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

### How it works
### TODO after merge

- Support preloading, see https://www.php.net/manual/en/class.ffi.php

- Rewrite the enum and doc generator in php.

- Add source/target API

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

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

### Test and install

Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
}
],
"require": {
"php": ">=7.2",
"ext-vips": ">=0.1.2",
"php": ">=7.4",
"ext-ffi": "*",
"psr/log": "^1.1.3|^2.0|^3.0"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpdocumentor/phpdocumentor": "3.0.0",
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.5"
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpdocumentor/shim": "^3.3",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
},
"autoload": {
"psr-4": {
Expand All @@ -39,7 +39,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "2.0.x-dev"
}
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"jcupitt/vips": "2.0.0"
}
}
44 changes: 44 additions & 0 deletions examples/watermark-image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

Vips\Config::setLogger(new Vips\DebugLogger());

if (count($argv) != 4) {
echo("usage: ./watermark.php input-image output-image watermark-image\n");
exit(1);
}

// we can stream the main image
$image = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);

// we'll read the watermark image many times, so we need random access for this
$watermark = Vips\Image::newFromFile($argv[3]);

// the watermark image needs to have an alpha channel
if (!$watermark->hasAlpha() || $watermark->bands != 4) {
echo("watermark image is not RGBA\n");
exit(1);
}

// make the watermark semi-transparent
$watermark = $watermark->multiply([1, 1, 1, 0.3])->cast("uchar");

// repeat the watermark to the size of the image
$watermark = $watermark->replicate(
1 + $image->width / $watermark->width,
1 + $image->height / $watermark->height
);
$watermark = $watermark->crop(0, 0, $image->width, $image->height);

// composite the watermark over the main image
$image = $image->composite2($watermark, 'over');

$image->writeToFile($argv[2]);

$image = null;
$watermark = null;

Vips\Config::shutDown();
4 changes: 3 additions & 1 deletion examples/watermark-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

#Vips\Config::setLogger(new Vips\DebugLogger());

if (count($argv) != 4) {
echo("usage: ./watermark-text.php input output \"some text\"\n");
exit(1);
Expand All @@ -30,7 +32,7 @@
$foreground = [255, 255, 255, 50];
$background = [0, 0, 255, 50];

// and a 10-pixel marghin
// and a 10-pixel margin
$margin = 10;

$overlay = $text_mask->ifthenelse($foreground, $background, [
Expand Down
69 changes: 69 additions & 0 deletions src/ArgumentFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* PHP version 7
*
* LICENSE:
*
* Copyright (c) 2016 John Cupitt
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/jcupitt/php-vips
*/

namespace Jcupitt\Vips;

/**
* The ArgumentFlags enum.
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/jcupitt/php-vips
*/
abstract class ArgumentFlags
{
const REQUIRED = 1;
const CONSTRUCT = 2;
const SET_ONCE = 4;
const SET_ALWAYS = 8;
const INPUT = 16;
const OUTPUT = 32;
const DEPRECATED = 64;
const MODIFY = 128;

const NAMES = [
"REQUIRED" => self::REQUIRED,
"CONSTRUCT" => self::CONSTRUCT,
"SET_ONCE" => self::SET_ONCE,
"SET_ALWAYS" => self::SET_ALWAYS,
"INPUT" => self::INPUT,
"OUTPUT" => self::OUTPUT,
"DEPRECATED" => self::DEPRECATED,
"MODIFY" => self::MODIFY,
];
}
Loading