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