Skip to content

Commit 1a3084c

Browse files
committed
add [] for extract band
1 parent 9b8d785 commit 1a3084c

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

Diff for: README.md

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Experimental PHP binding for libvips
22

3-
This is an experimental PHP binding for libvips. Not quite done yet, but
4-
it does more or less work.
3+
This is an experimental PHP binding for libvips.
54

65
### Example
76

@@ -10,7 +9,7 @@ it does more or less work.
109
<?php
1110
include 'vips.php';
1211

13-
$image = VImage::new_from_file($argv[1]);
12+
$image = VImage::new_from_file($argv[1]);
1413

1514
echo "width = ", $image->width, "\n";
1615

@@ -20,6 +19,25 @@ it does more or less work.
2019
?>
2120
```
2221

22+
### Missing
23+
24+
Some features that other vips bindings have are not yet implemented:
25+
26+
* You can't assign to an array index to change an image band.
27+
28+
* In-place operations, like `circle`, are not yet supported.
29+
30+
* No phpDoc yet.
31+
32+
* No exceptions yet.
33+
34+
* No logging at the moment.
35+
36+
Some things will never be done:
37+
38+
* You can't use `()` to get a pixel value, since php does not support multiple
39+
arguments to `__invoke()`.
40+
2341
### How it works
2442

2543
`vips.c` defines a simple but ugly route to call any libvips operation from PHP.
@@ -123,20 +141,6 @@ $ ./try1.php ~/pics/k2.jpg x.tif
123141

124142
See `examples/`.
125143

126-
### TODO
127-
128-
* `[]` for band extract should be easy
129-
130-
* can we overload `()` for `getpoint`? unclear
131-
132-
* add phpDoc comments
133-
134-
need comments for magic properties
135-
136-
extra phpDoc comments for all magic methods, use py to generate
137-
138-
* exceptions? logging?
139-
140144
### links
141145

142146
http://php.net/manual/en/internals2.php

Diff for: tests/041.phpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
VImage::[] works
3+
--SKIPIF--
4+
<?php if (!extension_loaded("vips")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
include 'vips.php';
8+
9+
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
10+
$image = VImage::new_from_file($filename);
11+
12+
$image = $image->invert()[1];
13+
14+
if ($image->bands == 1) {
15+
echo "pass";
16+
}
17+
?>
18+
--EXPECT--
19+
pass

Diff for: vips.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
dl('vips.' . PHP_SHLIB_SUFFIX);
99
}
1010

11-
class VImage
11+
class VImage implements ArrayAccess
1212
{
1313
/* The resource for the underlying VipsImage.
1414
*/
@@ -218,6 +218,29 @@ public static function __callStatic($name, $arguments)
218218
return self::call($name, NULL, $arguments);
219219
}
220220

221+
/* ArrayAccess interface ... we allow [] to get band.
222+
*/
223+
224+
public function offsetExists($offset)
225+
{
226+
return $offset >= 0 && $offset <= $this->bands - 1;
227+
}
228+
229+
public function offsetGet($offset)
230+
{
231+
return self::offsetExists($offset) ? self::extract_band($offset) : NULL;
232+
}
233+
234+
public function offsetSet($offset, $value)
235+
{
236+
echo "VImage::offsetSet: not implemented\n";
237+
}
238+
239+
public function offsetUnset($offset)
240+
{
241+
echo "VImage::offsetUnset: not implemented\n";
242+
}
243+
221244
/* add/sub/mul/div with a constant argument can use linear for a huge
222245
* speedup.
223246
*/

0 commit comments

Comments
 (0)