Skip to content

Commit 7cf9d49

Browse files
authored
use Target save to implement writeToBuffer (#207)
if possible
1 parent 85affa5 commit 7cf9d49

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

+2-2
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

+35-8
Original file line numberDiff line numberDiff line change
@@ -993,18 +993,45 @@ public function writeToBuffer(string $suffix, array $options = []): string
993993
$filename = Utils::filenameGetFilename($suffix);
994994
$string_options = Utils::filenameGetOptions($suffix);
995995

996-
$saver = FFI::vips()->vips_foreign_find_save_buffer($filename);
997-
if ($saver == "") {
998-
throw new Exception();
996+
$saver = null;
997+
998+
// see if we can save with the Target API ... we need 8.9 or later for
999+
// Target, and we need this libvips to have a target saver for this
1000+
// format
1001+
if (FFI::atLeast(8, 9)) {
1002+
FFI::vips()->vips_error_freeze();
1003+
$saver = FFI::vips()->vips_foreign_find_save_target($filename);
1004+
FFI::vips()->vips_error_thaw();
9991005
}
10001006

1001-
if (strlen($string_options) != 0) {
1002-
$options = array_merge([
1003-
"string_options" => $string_options,
1004-
], $options);
1007+
if ($saver !== null) {
1008+
$target = Target::newToMemory();
1009+
if (strlen($string_options) != 0) {
1010+
$options = array_merge([
1011+
"string_options" => $string_options,
1012+
], $options);
1013+
}
1014+
1015+
VipsOperation::call($saver, $this, [$target], $options);
1016+
1017+
$buffer = $target->get("blob");
1018+
} else {
1019+
// fall back to the old _buffer API
1020+
$saver = FFI::vips()->vips_foreign_find_save_buffer($filename);
1021+
if ($saver == "") {
1022+
throw new Exception();
1023+
}
1024+
1025+
if (strlen($string_options) != 0) {
1026+
$options = array_merge([
1027+
"string_options" => $string_options,
1028+
], $options);
1029+
}
1030+
1031+
$buffer = VipsOperation::call($saver, $this, [], $options);
10051032
}
10061033

1007-
return VipsOperation::call($saver, $this, [], $options);
1034+
return $buffer;
10081035
}
10091036

10101037
/**

0 commit comments

Comments
 (0)