diff --git a/examples/streaming-bench.php b/examples/streaming-bench.php old mode 100644 new mode 100755 diff --git a/examples/streaming.php b/examples/streaming.php old mode 100644 new mode 100755 diff --git a/src/GObject.php b/src/GObject.php index 964393a..40f34d9 100644 --- a/src/GObject.php +++ b/src/GObject.php @@ -223,7 +223,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure CData $hint, ?CData $data ) use (&$callback): void { - assert($numberOfParams === 0); + assert($numberOfParams === 1); /** * Signature: void(VipsTargetCustom* target, void* handle) */ @@ -242,7 +242,7 @@ private static function getMarshaler(string $name, callable $callback): ?Closure CData $hint, ?CData $data ) use (&$callback): void { - assert($numberOfParams === 0); + assert($numberOfParams === 1); /** * Signature: int(VipsTargetCustom* target, void* handle) */ diff --git a/src/Image.php b/src/Image.php index 1b6c257..049f2ca 100644 --- a/src/Image.php +++ b/src/Image.php @@ -983,18 +983,45 @@ public function writeToBuffer(string $suffix, array $options = []): string $filename = Utils::filenameGetFilename($suffix); $string_options = Utils::filenameGetOptions($suffix); - $saver = FFI::vips()->vips_foreign_find_save_buffer($filename); - if ($saver == "") { - throw new Exception(); + $saver = null; + + // see if we can save with the Target API ... we need 8.9 or later for + // Target, and we need this libvips to have a target saver for this + // format + if (FFI::atLeast(8, 9)) { + FFI::vips()->vips_error_freeze(); + $saver = FFI::vips()->vips_foreign_find_save_target($filename); + FFI::vips()->vips_error_thaw(); } - if (strlen($string_options) != 0) { - $options = array_merge([ - "string_options" => $string_options, - ], $options); + if ($saver !== null) { + $target = Target::newToMemory(); + if (strlen($string_options) != 0) { + $options = array_merge([ + "string_options" => $string_options, + ], $options); + } + + VipsOperation::call($saver, $this, [$target], $options); + + $buffer = $target->get("blob"); + } else { + // fall back to the old _buffer API + $saver = FFI::vips()->vips_foreign_find_save_buffer($filename); + if ($saver == "") { + throw new Exception(); + } + + if (strlen($string_options) != 0) { + $options = array_merge([ + "string_options" => $string_options, + ], $options); + } + + $buffer = VipsOperation::call($saver, $this, [], $options); } - return VipsOperation::call($saver, $this, [], $options); + return $buffer; } /**