Skip to content

Commit c107b66

Browse files
committed
Merge branch 'master' of github.com:libvips/php-vips
2 parents ccc7d95 + 908bf98 commit c107b66

23 files changed

+1293
-84
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ All notable changes to `:vips` will be documented in this file.
44

55
## master
66

7-
- improve FFI startup [West14]
7+
- improve FFI startup [West14, jcupitt]
88
- revise example use of composer [jcupitt]
99
- fix bandrank [axkirillov]
1010
- fix FFI startup log error [ganicus]
11+
- add Source, Target, SourceResource, TargetResource, SourceCustom,
12+
TargetCustom [L3tum]
13+
- add setProgress and progress example [jcupitt]
14+
- add streaming-custom example [jcupitt]
1115

1216
## 2.1.1 - 2022-11-13
1317

examples/generate_phpdoc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
GValue.array_int_type: 'integer[]|integer',
3232
GValue.array_double_type: 'float[]|float',
3333
GValue.array_image_type: 'Image[]|Image',
34-
GValue.blob_type: 'string'
34+
GValue.blob_type: 'string',
35+
GValue.source_type: 'Source',
36+
GValue.target_type: 'Target'
3537
}
3638

3739
# php result type names are different, annoyingly, and very restricted
@@ -48,7 +50,9 @@
4850
GValue.array_int_type: 'array',
4951
GValue.array_double_type: 'array',
5052
GValue.array_image_type: 'array',
51-
GValue.blob_type: 'string'
53+
GValue.blob_type: 'string',
54+
GValue.source_type: 'Source',
55+
GValue.target_type: 'Target'
5256
}
5357

5458
# values for VipsArgumentFlags

examples/progress.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
$image = Vips\Image::black(1, 1000000);
10+
$image->setProgress(true);
11+
12+
$image->signalConnect("preeval", function ($image, $progress) {
13+
echo "preeval:\n";
14+
});
15+
$image->signalConnect("eval", function ($image, $progress) {
16+
echo "eval: $progress->percent % complete\r";
17+
});
18+
19+
$image->signalConnect("posteval", function ($image, $progress) {
20+
echo "\nposteval:\n";
21+
});
22+
23+
// trigger evaluation
24+
$image->avg();
25+
26+
$image = null;
27+
28+
Vips\FFI::shutDown();

examples/streaming-bench.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Jcupitt\Vips\Config;
5+
use Jcupitt\Vips\Image;
6+
use Jcupitt\Vips\Source;
7+
use Jcupitt\Vips\SourceResource;
8+
use Jcupitt\Vips\Target;
9+
use Jcupitt\Vips\TargetResource;
10+
11+
require dirname(__DIR__) . '/vendor/autoload.php';
12+
13+
$doBenchmark = static function () {
14+
$sourceOptions = ['access' => 'sequential'];
15+
$sourceOptionString = 'access=sequential';
16+
$iterations = 100;
17+
$targetWidth = 100.0;
18+
$targetSuffix = '.jpg';
19+
$targetOptions = ['optimize-coding' => true, 'strip' => true, 'Q' => 100, 'profile' => 'srgb'];
20+
$targetFile = dirname(__DIR__) . "/tests/images/target.jpg";
21+
$sourceFile = dirname(__DIR__) . '/tests/images/img_0076.jpg';
22+
23+
### Callbacks
24+
$start = microtime(true);
25+
26+
for ($i = 0; $i < $iterations; $i++) {
27+
$source = new SourceResource(fopen($sourceFile, 'rb'));
28+
$target = new TargetResource(fopen($targetFile, 'wb+'));
29+
$image = Image::newFromSource($source, '', $sourceOptions);
30+
$image = $image->resize($targetWidth / $image->width);
31+
$image->writeToTarget(
32+
$target,
33+
$targetSuffix,
34+
$targetOptions
35+
);
36+
unlink($targetFile);
37+
}
38+
39+
echo (microtime(true) - $start) . ' Seconds for Streaming with callbacks' . PHP_EOL;
40+
41+
### Builtin
42+
$start = microtime(true);
43+
44+
for ($i = 0; $i < $iterations; $i++) {
45+
$source = Source::newFromFile($sourceFile);
46+
$target = Target::newToFile($targetFile);
47+
$image = Image::newFromSource($source, '', $sourceOptions);
48+
$image = $image->resize($targetWidth / $image->width);
49+
$image->writeToTarget(
50+
$target,
51+
$targetSuffix,
52+
$targetOptions
53+
);
54+
unlink($targetFile);
55+
}
56+
57+
echo (microtime(true) - $start) . ' Seconds for Streaming with builtin source/target' . PHP_EOL;
58+
59+
### Callbacks Thumbnail
60+
$start = microtime(true);
61+
62+
for ($i = 0; $i < $iterations; $i++) {
63+
$source = new SourceResource(fopen($sourceFile, 'rb'));
64+
$target = new TargetResource(fopen($targetFile, 'wb+'));
65+
$image = Image::thumbnail_source($source, $targetWidth);
66+
$image->writeToTarget(
67+
$target,
68+
$targetSuffix,
69+
$targetOptions
70+
);
71+
unlink($targetFile);
72+
}
73+
74+
echo (microtime(true) - $start) . ' Seconds for Streaming Thumbnail with callbacks' . PHP_EOL;
75+
76+
### Builtin Thumbnail
77+
$start = microtime(true);
78+
79+
for ($i = 0; $i < $iterations; $i++) {
80+
$source = Source::newFromFile($sourceFile);
81+
$target = Target::newToFile($targetFile);
82+
$image = Image::thumbnail_source($source, $targetWidth);
83+
$image->writeToTarget(
84+
$target,
85+
$targetSuffix,
86+
$targetOptions
87+
);
88+
unlink($targetFile);
89+
}
90+
91+
echo (microtime(true) - $start) . ' Seconds for Streaming Thumbnail with builtin source/target' . PHP_EOL;
92+
93+
### Thumbnail
94+
$start = microtime(true);
95+
96+
for ($i = 0; $i < $iterations; $i++) {
97+
$image = Image::thumbnail($sourceFile . "[$sourceOptionString]", $targetWidth);
98+
$image->writeToFile(
99+
$targetFile,
100+
$targetOptions
101+
);
102+
unlink($targetFile);
103+
}
104+
105+
echo (microtime(true) - $start) . ' Seconds for Thumbnail API' . PHP_EOL;
106+
107+
### Classic
108+
$start = microtime(true);
109+
110+
for ($i = 0; $i < $iterations; $i++) {
111+
$image = Image::newFromFile($sourceFile, $sourceOptions);
112+
$image = $image->resize($targetWidth / $image->width);
113+
$image->writeToFile(
114+
$targetFile,
115+
$targetOptions
116+
);
117+
unlink($targetFile);
118+
}
119+
120+
echo (microtime(true) - $start) . ' Seconds for Classic API' . PHP_EOL;
121+
};
122+
123+
$doBenchmark();
124+
125+
//echo "=== NOW NO CACHE ===" . PHP_EOL;
126+
//
127+
//Config::cacheSetMax(0);
128+
//Config::cacheSetMaxFiles(0);
129+
//Config::cacheSetMaxMem(0);
130+
//
131+
//$doBenchmark();

examples/streaming-custom.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require dirname(__DIR__) . '/vendor/autoload.php';
5+
6+
use Jcupitt\Vips;
7+
8+
if (count($argv) != 4) {
9+
echo "usage: $argv[0] IN-FILE OUT-FILE FORMAT\n";
10+
echo " eg.: $argv[0] ~/pics/k2.jpg x.tif .tif[tile,pyramid]\n";
11+
exit(1);
12+
}
13+
14+
$in_file = fopen($argv[1], 'r');
15+
$source = new Vips\SourceCustom();
16+
$source->onRead(function ($bufferLength) use (&$in_file) {
17+
// return 0 for EOF, -ve for read error
18+
return fread($in_file, $bufferLength);
19+
});
20+
// seek is optional
21+
$source->onSeek(function ($offset, $whence) use (&$in_file) {
22+
if (fseek($in_file, $offset, $whence)) {
23+
return -1;
24+
}
25+
26+
return ftell($in_file);
27+
});
28+
29+
// open for write and read ... formats like tiff need to be able to seek back
30+
// in the output and update bytes later
31+
$out_file = fopen($argv[2], 'w+');
32+
$target = new Vips\TargetCustom();
33+
$target->onWrite(function ($buffer) use (&$out_file) {
34+
$result = fwrite($out_file, $buffer);
35+
if ($result === false) {
36+
// IO error
37+
return -1;
38+
} else {
39+
return $result;
40+
}
41+
});
42+
// read and seek are optional
43+
$target->onSeek(function ($offset, $whence) use (&$out_file) {
44+
if (fseek($out_file, $offset, $whence)) {
45+
return -1;
46+
}
47+
48+
return ftell($out_file);
49+
});
50+
$target->onRead(function ($bufferLength) use (&$out_file) {
51+
return fread($out_file, $bufferLength);
52+
});
53+
54+
$image = Vips\Image::newFromSource($source);
55+
$image->writeToTarget($target, $argv[3]);

examples/streaming.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require dirname(__DIR__) . '/vendor/autoload.php';
5+
6+
use Jcupitt\Vips;
7+
use Jcupitt\Vips\Source;
8+
9+
$source = Source::newFromFile(dirname(__DIR__) . '/tests/images/img_0076.jpg');
10+
$target = Vips\Target::newToFile(dirname(__DIR__) . "/tests/images/target.jpg");
11+
$image = Vips\Image::newFromSource($source);
12+
$image->writeToTarget($target, '.jpg[Q=95]');

src/Connection.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Jcupitt\Vips;
4+
5+
abstract class Connection extends VipsObject
6+
{
7+
/**
8+
* A pointer to the underlying Connection. This is the same as the
9+
* GObject, just cast to Connection to help FFI.
10+
*
11+
* @internal
12+
*/
13+
public \FFI\CData $pointer;
14+
15+
public function __construct(\FFI\CData $pointer)
16+
{
17+
$this->pointer = \FFI::cast(FFI::ctypes('VipsConnection'), $pointer);
18+
parent::__construct($pointer);
19+
}
20+
21+
/**
22+
* Get the filename associated with a connection. Return null if there is no associated file.
23+
*/
24+
public function filename(): ?string
25+
{
26+
return FFI::vips()->vips_connection_filename($this->pointer);
27+
}
28+
29+
/**
30+
* Make a human-readable name for a connection suitable for error messages.
31+
*/
32+
public function nick(): ?string
33+
{
34+
return FFI::vips()->vips_connection_nick($this->pointer);
35+
}
36+
}

0 commit comments

Comments
 (0)