Skip to content

Commit

Permalink
use Target save to implement writeToBuffer (#207)
Browse files Browse the repository at this point in the history
if possible
  • Loading branch information
jcupitt authored Feb 14, 2024
1 parent 85affa5 commit 7cf9d49
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Empty file modified examples/streaming-bench.php
100644 → 100755
Empty file.
Empty file modified examples/streaming.php
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/GObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand All @@ -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)
*/
Expand Down
43 changes: 35 additions & 8 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,18 +993,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;
}

/**
Expand Down

0 comments on commit 7cf9d49

Please sign in to comment.