Skip to content

Commit 97c3fd8

Browse files
committed
add getFields()
and an example program see #218
1 parent ce833a6 commit 97c3fd8

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to `:vips` will be documented in this file.
44

55
## master
66

7+
- add getFields() to fetch an array of field names
8+
79
## 2.2.0 - 2023-08-17
810

911
- improve FFI startup [West14, jcupitt]

examples/fields.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require dirname(__DIR__) . '/vendor/autoload.php';
5+
6+
use Jcupitt\Vips;
7+
8+
$im = Vips\Image::newFromFile($argv[1]);
9+
10+
$names = $im->getFields();
11+
12+
echo "$argv[1]\n";
13+
foreach ($names as &$name) {
14+
$value = $im->get($name);
15+
echo "$name: $value\n";
16+
}
17+
18+
/*
19+
* Local variables:
20+
* tab-width: 4
21+
* c-basic-offset: 4
22+
* End:
23+
* vim600: expandtab sw=4 ts=4 fdm=marker
24+
* vim<600: expandtab sw=4 ts=4
25+
*/

src/FFI.php

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ private static function init(): void
348348
$glib_decls = $typedefs . <<<EOS
349349
void* g_malloc (size_t size);
350350
void g_free (void* data);
351+
void g_strfreev (char** str_array);
351352
EOS;
352353

353354
// GObject declarations

src/Image.php

+21
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
* conform to PHP naming conventions, you can use something like
115115
* `$image->get('ipct-data')`.
116116
*
117+
* Use `$image->getFields()` to get an array of all the possible field names.
118+
*
117119
* Next we have:
118120
*
119121
* ```php
@@ -1217,6 +1219,25 @@ public function typeOf(string $name): int
12171219
return $this->getType($name);
12181220
}
12191221

1222+
/**
1223+
* Get the field names available for an image.
1224+
*
1225+
* @return Array
1226+
*/
1227+
public function getFields(): array
1228+
{
1229+
$str_array = FFI::vips()->vips_image_get_fields($this->pointer);
1230+
1231+
$fields = [];
1232+
for ($i = 0; $str_array[$i] != null; $i++) {
1233+
array_push($fields, \FFI::string($str_array[$i]));
1234+
}
1235+
1236+
FFI::glib()->g_free($str_array);
1237+
1238+
return $fields;
1239+
}
1240+
12201241
/**
12211242
* Set any property on the underlying image.
12221243
*

0 commit comments

Comments
 (0)