@@ -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