Skip to content

Commit b15049b

Browse files
committed
add streaming-custom example
See #191
1 parent bbedf8d commit b15049b

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
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/streaming-custom.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
39+
else
40+
return $result;
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]);
56+

src/GObject.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
151151
CData $hint,
152152
?CData $data
153153
) use (&$callback): void {
154-
assert($numberOfParams === 4);
154+
assert($numberOfParams === 3);
155155
/*
156156
* Signature: gint64(VipsSourceCustom* source, void* buffer, gint64 length, void* handle)
157157
*/
@@ -179,7 +179,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
179179
CData $hint,
180180
?CData $data
181181
) use (&$callback): void {
182-
assert($numberOfParams === 4);
182+
assert($numberOfParams === 3);
183183
/*
184184
* Signature: gint64(VipsSourceCustom* source, gint64 offset, int whence, void* handle)
185185
*/
@@ -200,7 +200,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
200200
CData $hint,
201201
?CData $data
202202
) use (&$callback): void {
203-
assert($numberOfParams === 4);
203+
assert($numberOfParams === 3);
204204
/*
205205
* Signature: gint64(VipsTargetCustom* target, void* buffer, gint64 length, void* handle)
206206
*/
@@ -223,7 +223,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
223223
CData $hint,
224224
?CData $data
225225
) use (&$callback): void {
226-
assert($numberOfParams === 2);
226+
assert($numberOfParams === 0);
227227
/**
228228
* Signature: void(VipsTargetCustom* target, void* handle)
229229
*/
@@ -242,7 +242,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
242242
CData $hint,
243243
?CData $data
244244
) use (&$callback): void {
245-
assert($numberOfParams === 2);
245+
assert($numberOfParams === 0);
246246
/**
247247
* Signature: int(VipsTargetCustom* target, void* handle)
248248
*/

0 commit comments

Comments
 (0)