Skip to content

Commit 85affa5

Browse files
committed
add flags support
see #229
1 parent bbeef60 commit 85affa5

File tree

7 files changed

+151
-35
lines changed

7 files changed

+151
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to `:vips` will be documented in this file.
55
## master
66

77
- added `animate-image.php` example [jcupitt]
8+
- added flags support [jcupitt]
9+
- added `keep.php` example [jcupitt]
810

911
## 2.3.0 - 2023-09-26
1012

examples/fields.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55

66
use Jcupitt\Vips;
77

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";
8+
function printMetadata($im)
9+
{
10+
foreach ($im->getFields() as &$name) {
11+
$value = $im->get($name);
12+
if (str_ends_with($name, "-data")) {
13+
$len = strlen($value);
14+
$value = "<$len bytes of binary data>";
15+
}
16+
echo " $name: $value\n";
17+
}
1618
}
1719

20+
$im = Vips\Image::newFromFile($argv[1]);
21+
printMetadata($im);
22+
1823
/*
1924
* Local variables:
2025
* tab-width: 4

examples/generate_phpdoc.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/python3
22

3+
# needs pyvips 2.2.3 or later
4+
35
from pyvips import Image, Introspect, GValue, Error, \
4-
ffi, values_for_enum, vips_lib, gobject_lib, \
6+
ffi, enum_dict, flags_dict, vips_lib, gobject_lib, \
57
type_map, type_name, type_from_name, nickname_find
68

79
# This file generates the phpdoc comments for the magic methods and properties.
8-
# It's in Python, since we use the whole of FFI, not just the
9-
# small bit exposed by php-vips-ext.
1010

1111
# Regenerate docs with something like:
1212
#
@@ -292,6 +292,10 @@ def add_enum(gtype, a, b):
292292

293293
type_map(type_from_name('GEnum'), add_enum)
294294

295+
# Filter internal enums
296+
blacklist = ['VipsImageType', 'VipsToken']
297+
all_enums = [name for name in all_enums if name not in blacklist]
298+
295299
for name in all_enums:
296300
gtype = type_from_name(name)
297301
php_name = remove_prefix(name)
@@ -310,14 +314,59 @@ def add_enum(gtype, a, b):
310314
f.write('abstract class {0}\n'.format(php_name))
311315
f.write('{\n')
312316

313-
for value in values_for_enum(gtype):
314-
php_name = value.replace('-', '_').upper()
317+
for key, value in enum_dict(gtype).items():
318+
php_name = key.replace('-', '_').upper()
319+
if php_name in reserved_php_names:
320+
php_name = reserved_php_names[php_name]
321+
f.write(' const {0} = \'{1}\';\n'.format(php_name, key))
322+
323+
f.write('}\n')
324+
325+
326+
def generate_flags():
327+
all_flags = []
328+
329+
def add_flags(gtype, a, b):
330+
nickname = type_name(gtype)
331+
all_flags.append(nickname)
332+
333+
type_map(gtype, add_flags)
334+
335+
return ffi.NULL
336+
337+
type_map(type_from_name('GFlags'), add_flags)
338+
339+
# Filter internal flags
340+
blacklist = ['VipsForeignFlags']
341+
all_flags = [name for name in all_flags if name not in blacklist]
342+
343+
for name in all_flags:
344+
gtype = type_from_name(name)
345+
php_name = remove_prefix(name)
346+
347+
print('Generating {0}.php ...'.format(php_name))
348+
349+
with open('{0}.php'.format(php_name), 'w') as f:
350+
f.write(preamble)
351+
f.write('\n')
352+
f.write('namespace Jcupitt\\Vips;\n')
353+
f.write('\n')
354+
f.write('/**\n')
355+
f.write(' * The {0} flags.\n'.format(php_name))
356+
f.write(class_header)
357+
f.write(' */\n')
358+
f.write('abstract class {0}\n'.format(php_name))
359+
f.write('{\n')
360+
361+
for key, value in flags_dict(gtype).items():
362+
php_name = key.replace('-', '_').upper()
315363
if php_name in reserved_php_names:
316364
php_name = reserved_php_names[php_name]
317-
f.write(' const {0} = \'{1}\';\n'.format(php_name, value))
365+
f.write(' const {0} = {1};\n'.format(php_name, value))
318366

319367
f.write('}\n')
320368

321369

322370
generate_auto_doc('ImageAutodoc.php')
323371
generate_enums()
372+
generate_flags()

examples/keep.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require dirname(__DIR__) . '/vendor/autoload.php';
5+
6+
use Jcupitt\Vips;
7+
8+
function printMetadata($im)
9+
{
10+
foreach ($im->getFields() as &$name) {
11+
$value = $im->get($name);
12+
if (str_ends_with($name, "-data")) {
13+
$len = strlen($value);
14+
$value = "<$len bytes of binary data>";
15+
}
16+
echo " $name: $value\n";
17+
}
18+
}
19+
20+
$im = Vips\Image::newFromFile($argv[1]);
21+
echo "$argv[1]\n";
22+
printMetadata($im);
23+
24+
echo "\nafter keep => icc\n";
25+
$buf = $im->tiffsave_buffer(['keep' => Vips\ForeignKeep::ICC]);
26+
$im2 = Vips\Image::newFromBuffer($buf, "");
27+
printMetadata($im2);
28+
29+
echo "\nafter keep => exif|xmp\n";
30+
$buf = $im->tiffsave_buffer(['keep' => Vips\ForeignKeep::ICC | Vips\ForeignKeep::XMP]);
31+
$im2 = Vips\Image::newFromBuffer($buf, "");
32+
printMetadata($im2);
33+
34+
/*
35+
* Local variables:
36+
* tab-width: 4
37+
* c-basic-offset: 4
38+
* End:
39+
* vim600: expandtab sw=4 ts=4 fdm=marker
40+
* vim<600: expandtab sw=4 ts=4
41+
*/

src/ImageType.php renamed to src/ForeignKeep.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@
3939
namespace Jcupitt\Vips;
4040

4141
/**
42-
* The ImageType enum.
42+
* The ForeignKeep flags.
4343
* @category Images
4444
* @package Jcupitt\Vips
4545
* @author John Cupitt <jcupitt@gmail.com>
4646
* @copyright 2016 John Cupitt
4747
* @license https://opensource.org/licenses/MIT MIT
4848
* @link https://github.com/jcupitt/php-vips
4949
*/
50-
abstract class ImageType
50+
abstract class ForeignKeep
5151
{
52-
const ERROR = 'error';
53-
const NONE = 'none';
54-
const SETBUF = 'setbuf';
55-
const SETBUF_FOREIGN = 'setbuf-foreign';
56-
const OPENIN = 'openin';
57-
const MMAPIN = 'mmapin';
58-
const MMAPINRW = 'mmapinrw';
59-
const OPENOUT = 'openout';
52+
const NONE = 0;
53+
const EXIF = 1;
54+
const XMP = 2;
55+
const IPTC = 4;
56+
const ICC = 8;
57+
const OTHER = 16;
58+
const ALL = 31;
6059
}

src/Token.php renamed to src/ForeignPngFilter.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@
3939
namespace Jcupitt\Vips;
4040

4141
/**
42-
* The Token enum.
42+
* The ForeignPngFilter flags.
4343
* @category Images
4444
* @package Jcupitt\Vips
4545
* @author John Cupitt <jcupitt@gmail.com>
4646
* @copyright 2016 John Cupitt
4747
* @license https://opensource.org/licenses/MIT MIT
4848
* @link https://github.com/jcupitt/php-vips
4949
*/
50-
abstract class Token
50+
abstract class ForeignPngFilter
5151
{
52-
const LEFT = 'left';
53-
const RIGHT = 'right';
54-
const STRING = 'string';
55-
const EQUALS = 'equals';
52+
const NONE = 8;
53+
const SUB = 16;
54+
const UP = 32;
55+
const AVG = 64;
56+
const PAETH = 128;
57+
const ALL = 248;
5658
}

src/ImageAutodoc.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
* @throws Exception
161161
* @method Image convi(Image $mask, array $options = []) Int convolution operation.
162162
* @throws Exception
163-
* @method Image convsep(Image $mask, array $options = []) Seperable convolution operation.
163+
* @method Image convsep(Image $mask, array $options = []) Separable convolution operation.
164164
* @throws Exception
165165
* @method Image copy(array $options = []) Copy an image.
166166
* @throws Exception
@@ -372,6 +372,18 @@
372372
* @throws Exception
373373
* @method void jxlsave_target(Target $target, array $options = []) Save image in JPEG-XL format.
374374
* @throws Exception
375+
* @method static Image kakaduload(string $filename, array $options = []) Load JPEG2000 image.
376+
* @throws Exception
377+
* @method static Image kakaduload_buffer(string $buffer, array $options = []) Load JPEG2000 image.
378+
* @throws Exception
379+
* @method static Image kakaduload_source(Source $source, array $options = []) Load JPEG2000 image.
380+
* @throws Exception
381+
* @method void kakadusave(string $filename, array $options = []) Save image in JPEG2000 format.
382+
* @throws Exception
383+
* @method string kakadusave_buffer(array $options = []) Save image in JPEG2000 format.
384+
* @throws Exception
385+
* @method void kakadusave_target(Target $target, array $options = []) Save image in JPEG2000 format.
386+
* @throws Exception
375387
* @method Image labelregions(array $options = []) Label regions in an image.
376388
* @throws Exception
377389
* @method Image linear(float[]|float $a, float[]|float $b, array $options = []) Calculate (a * in + b).
@@ -380,9 +392,9 @@
380392
* @throws Exception
381393
* @method static Image logmat(float $sigma, float $min_ampl, array $options = []) Make a Laplacian of Gaussian image.
382394
* @throws Exception
383-
* @method static Image magickload(string $filename, array $options = []) Load file with ImageMagick7.
395+
* @method static Image magickload(string $filename, array $options = []) Load file with ImageMagick.
384396
* @throws Exception
385-
* @method static Image magickload_buffer(string $buffer, array $options = []) Load buffer with ImageMagick7.
397+
* @method static Image magickload_buffer(string $buffer, array $options = []) Load buffer with ImageMagick.
386398
* @throws Exception
387399
* @method void magicksave(string $filename, array $options = []) Save file with ImageMagick.
388400
* @throws Exception
@@ -457,6 +469,12 @@
457469
* @throws Exception
458470
* @method Image msb(array $options = []) Pick most-significant byte from an image.
459471
* @throws Exception
472+
* @method static Image niftiload(string $filename, array $options = []) Load NIfTI volume.
473+
* @throws Exception
474+
* @method static Image niftiload_source(Source $source, array $options = []) Load NIfTI volumes.
475+
* @throws Exception
476+
* @method void niftisave(string $filename, array $options = []) Save image to nifti file.
477+
* @throws Exception
460478
* @method static Image openexrload(string $filename, array $options = []) Load an OpenEXR image.
461479
* @throws Exception
462480
* @method static Image openslideload(string $filename, array $options = []) Load file with OpenSlide.
@@ -481,9 +499,9 @@
481499
* @throws Exception
482500
* @method static Image pngload_source(Source $source, array $options = []) Load png from source.
483501
* @throws Exception
484-
* @method void pngsave(string $filename, array $options = []) Save image to file as PNG.
502+
* @method void pngsave(string $filename, array $options = []) Save image to png file.
485503
* @throws Exception
486-
* @method string pngsave_buffer(array $options = []) Save image to buffer as PNG.
504+
* @method string pngsave_buffer(array $options = []) Save image to png buffer.
487505
* @throws Exception
488506
* @method void pngsave_target(Target $target, array $options = []) Save image to target as PNG.
489507
* @throws Exception

0 commit comments

Comments
 (0)