Skip to content

Commit e764056

Browse files
committed
more examples
1 parent 259710c commit e764056

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Diff for: examples/progress.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/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();

Diff for: examples/streaming-custom.php

+55
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\VipsSourceCustom();
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\VipsTargetCustom();
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]);

Diff for: tests/images/target.jpg

1.08 MB
Loading

0 commit comments

Comments
 (0)