Skip to content

Commit 254c7bc

Browse files
committed
use Target save to implement writeToBuffer
if possible
1 parent 908bf98 commit 254c7bc

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

examples/streaming-bench.php

100644100755
File mode changed.

examples/streaming.php

100644100755
File mode changed.

src/GObject.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
223223
CData $hint,
224224
?CData $data
225225
) use (&$callback): void {
226-
assert($numberOfParams === 0);
226+
assert($numberOfParams === 1);
227227
/**
228228
* Signature: void(VipsTargetCustom* target, void* handle)
229229
*/
@@ -242,7 +242,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure
242242
CData $hint,
243243
?CData $data
244244
) use (&$callback): void {
245-
assert($numberOfParams === 0);
245+
assert($numberOfParams === 1);
246246
/**
247247
* Signature: int(VipsTargetCustom* target, void* handle)
248248
*/

src/Image.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -983,18 +983,45 @@ public function writeToBuffer(string $suffix, array $options = []): string
983983
$filename = Utils::filenameGetFilename($suffix);
984984
$string_options = Utils::filenameGetOptions($suffix);
985985

986-
$saver = FFI::vips()->vips_foreign_find_save_buffer($filename);
987-
if ($saver == "") {
988-
throw new Exception();
986+
$saver = null;
987+
988+
// see if we can save with the Target API ... we need 8.9 or later for
989+
// Target, and we need this libvips to have a target saver for this
990+
// format
991+
if (FFI::atLeast(8, 9)) {
992+
FFI::vips()->vips_error_freeze();
993+
$saver = FFI::vips()->vips_foreign_find_save_target($filename);
994+
FFI::vips()->vips_error_thaw();
989995
}
990996

991-
if (strlen($string_options) != 0) {
992-
$options = array_merge([
993-
"string_options" => $string_options,
994-
], $options);
997+
if ($saver !== null) {
998+
$target = Target::newToMemory();
999+
if (strlen($string_options) != 0) {
1000+
$options = array_merge([
1001+
"string_options" => $string_options,
1002+
], $options);
1003+
}
1004+
1005+
VipsOperation::call($saver, $this, [$target], $options);
1006+
1007+
$buffer = $target->get("blob");
1008+
} else {
1009+
// fall back to the old _buffer API
1010+
$saver = FFI::vips()->vips_foreign_find_save_buffer($filename);
1011+
if ($saver == "") {
1012+
throw new Exception();
1013+
}
1014+
1015+
if (strlen($string_options) != 0) {
1016+
$options = array_merge([
1017+
"string_options" => $string_options,
1018+
], $options);
1019+
}
1020+
1021+
$buffer = VipsOperation::call($saver, $this, [], $options);
9951022
}
9961023

997-
return VipsOperation::call($saver, $this, [], $options);
1024+
return $buffer;
9981025
}
9991026

10001027
/**

0 commit comments

Comments
 (0)