Skip to content

Commit a852510

Browse files
committed
fix: Some cleanup and making pointers usable
1 parent dc46e1c commit a852510

File tree

11 files changed

+207
-23
lines changed

11 files changed

+207
-23
lines changed

src/Connection.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44

55
abstract class Connection extends VipsObject
66
{
7+
/**
8+
* A pointer to the underlying Connection. This is the same as the
9+
* GObject, just cast to Connection to help FFI.
10+
*
11+
* @internal
12+
*/
13+
public \FFI\CData $pointer;
14+
15+
public function __construct(\FFI\CData $pointer)
16+
{
17+
$this->pointer = \FFI::cast(FFI::ctypes('VipsConnection'), $pointer);
18+
parent::__construct($pointer);
19+
}
20+
721
/**
822
* Get the filename associated with a connection. Return null if there is no associated file.
923
*/
1024
public function filename(): ?string
1125
{
12-
$so = \FFI::cast(FFI::ctypes('VipsConnection'), $this->pointer);
13-
$pointer = FFI::vips()->vips_connection_filename($so);
14-
15-
if (\FFI::isNull($pointer)) {
16-
return null;
17-
}
18-
19-
return \FFI::string($pointer);
26+
return FFI::vips()->vips_connection_filename($this->pointer);
2027
}
2128

2229
/**
2330
* Make a human-readable name for a connection suitable for error messages.
2431
*/
2532
public function nick(): ?string
2633
{
27-
$so = \FFI::cast(FFI::ctypes('VipsConnection'), $this->pointer);
28-
$pointer = FFI::vips()->vips_connection_nick($so);
29-
30-
if (\FFI::isNull($pointer)) {
31-
return null;
32-
}
33-
34-
return \FFI::string($pointer);
34+
return FFI::vips()->vips_connection_nick($this->pointer);
3535
}
3636
}

src/FFI.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,11 @@ private static function init(): void
751751
"VipsOperation" => self::$vips->type("VipsOperation*"),
752752
"VipsImage" => self::$vips->type("VipsImage*"),
753753
"VipsInterpolate" => self::$vips->type("VipsInterpolate*"),
754+
"VipsConnection" => self::$vips->type("VipsConnection*"),
755+
"VipsSource" => self::$vips->type("VipsSource*"),
756+
"VipsSourceCustom" => self::$vips->type("VipsSourceCustom*"),
757+
"VipsTarget" => self::$vips->type("VipsTarget*"),
758+
"VipsTargetCustom" => self::$vips->type("VipsTargetCustom*"),
754759
];
755760

756761
self::$gtypes = [
@@ -774,6 +779,8 @@ private static function init(): void
774779

775780
"GObject" => self::$gobject->g_type_from_name("GObject"),
776781
"VipsImage" => self::$gobject->g_type_from_name("VipsImage"),
782+
783+
"GCallback" => self::$gobject->g_type_from_name("GCallback"),
777784
];
778785

779786
// map vips format names to c type names

src/Image.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,38 @@ public function newFromImage($value): Image
901901
]);
902902
}
903903

904+
/**
905+
* Find the name of the load operation vips will use to load a VipsSource, for
906+
* example 'VipsForeignLoadJpegSource'. You can use this to work out what
907+
* options to pass to newFromSource().
908+
*
909+
* @param VipsSource $source The source to test
910+
* @return string|null The name of the load operation, or null.
911+
*/
912+
public static function findLoadSource(VipsSource $source): ?string
913+
{
914+
return FFI::vips()->vips_foreign_find_load_source(\FFI::cast(FFI::ctypes('VipsSource'), $source->pointer));
915+
}
916+
917+
/**
918+
* @throws Exception
919+
*/
920+
public static function newFromSource(VipsSource $source, string $string_options = '', array $options = []): self
921+
{
922+
$loader = self::findLoadSource($source);
923+
if ($loader === null) {
924+
throw new Exception('unable to load from source');
925+
}
926+
927+
if ($string_options !== '') {
928+
$options = array_merge([
929+
"string_options" => $string_options,
930+
], $options);
931+
}
932+
933+
return VipsOperation::call($loader, null, [$source], $options);
934+
}
935+
904936
/**
905937
* Write an image to a file.
906938
*
@@ -1040,6 +1072,28 @@ public function writeToArray(): array
10401072
return $result;
10411073
}
10421074

1075+
/**
1076+
* @throws Exception
1077+
*/
1078+
public function writeToTarget(VipsTarget $target, string $suffix, array $options = []): void
1079+
{
1080+
$filename = Utils::filenameGetFilename($suffix);
1081+
$string_options = Utils::filenameGetOptions($suffix);
1082+
$saver = FFI::vips()->vips_foreign_find_save_target($filename);
1083+
1084+
if ($saver === '') {
1085+
throw new Exception("can't save to target with filename $filename");
1086+
}
1087+
1088+
if ($string_options !== '') {
1089+
$options = array_merge([
1090+
"string_options" => $string_options,
1091+
], $options);
1092+
}
1093+
1094+
VipsOperation::call($saver, $this, [$target], $options);
1095+
}
1096+
10431097
/**
10441098
* Copy to memory.
10451099
*

src/VipsObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ abstract class VipsObject extends GObject
5656
*
5757
* @internal
5858
*/
59-
protected \FFI\CData $pointer;
59+
private \FFI\CData $pointer;
6060

6161
/**
6262
* A pointer to the underlying GObject. This is the same as the

src/VipsSource.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
class VipsSource extends Connection
66
{
7+
/**
8+
* A pointer to the underlying VipsSource. This is the same as the
9+
* GObject, just cast to VipsSource to help FFI.
10+
*
11+
* @internal
12+
*/
13+
public \FFI\CData $pointer;
14+
15+
public function __construct(\FFI\CData $pointer)
16+
{
17+
$this->pointer = \FFI::cast(FFI::ctypes('VipsSource'), $pointer);
18+
parent::__construct($pointer);
19+
}
20+
721
/**
822
* Make a new source from a file descriptor (a small integer).
923
* Make a new source that is attached to the descriptor. For example:

src/VipsSourceCustom.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
class VipsSourceCustom extends VipsSource
88
{
9+
/**
10+
* A pointer to the underlying VipsSourceCustom. This is the same as the
11+
* GObject, just cast to VipsSourceCustom to help FFI.
12+
*
13+
* @internal
14+
*/
15+
public \FFI\CData $pointer;
16+
917
public function __construct()
1018
{
11-
$source = \FFI::cast(FFI::ctypes('VipsSource'), FFI::vips()->vips_source_custom_new());
12-
parent::__construct($source);
19+
$this->pointer = FFI::vips()->vips_source_custom_new();
20+
parent::__construct($this->pointer);
1321
}
1422

1523
/**

src/VipsSourceResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class VipsSourceResource extends VipsSourceCustom
1212
private $resource;
1313

1414
/**
15-
* The resource passed in will become "owned" by this class. On destruction of this class, the resource will be closed.
15+
* The resource passed in will become "owned" by this class.
16+
* On destruction of this class, the resource will be closed.
1617
*
1718
* @param resource $resource
1819
*/

src/VipsTarget.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
class VipsTarget extends Connection
66
{
7+
/**
8+
* A pointer to the underlying VipsTarget. This is the same as the
9+
* GObject, just cast to VipsTarget to help FFI.
10+
*
11+
* @internal
12+
*/
13+
public \FFI\CData $pointer;
14+
15+
public function __construct(\FFI\CData $pointer)
16+
{
17+
$this->pointer = \FFI::cast(FFI::ctypes('VipsTarget'), $pointer);
18+
parent::__construct($pointer);
19+
}
20+
721
public static function newToDescriptor(int $descriptor): self
822
{
923
$pointer = FFI::vips()->vips_target_new_to_descriptor($descriptor);

src/VipsTargetCustom.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
class VipsTargetCustom extends VipsTarget
88
{
9+
/**
10+
* A pointer to the underlying VipsTargetCustom. This is the same as the
11+
* GObject, just cast to VipsTargetCustom to help FFI.
12+
*
13+
* @internal
14+
*/
15+
public \FFI\CData $pointer;
16+
917
public function __construct()
1018
{
11-
$pointer = \FFI::cast(FFI::ctypes('VipsTarget'), FFI::vips()->vips_target_custom_new());
12-
parent::__construct($pointer);
19+
$this->pointer = FFI::vips()->vips_target_custom_new();
20+
parent::__construct($this->pointer);
1321
}
1422

1523
public function onWrite(Closure $callback): void

src/VipsTargetResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class VipsTargetResource extends VipsTargetCustom
1010
private $resource;
1111

1212
/**
13-
* The resource passed in will become "owned" by this class. On destruction of this class, the resource will be closed.
13+
* The resource passed in will become "owned" by this class.
14+
* On destruction of this class, the resource will be closed.
1415
*
1516
* @param resource $resource
1617
*/

0 commit comments

Comments
 (0)