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