@@ -12,16 +12,6 @@ class Source extends Connection
12
12
*/
13
13
public \FFI \CData $ pointer ;
14
14
15
- /**
16
- * Pointer to the underlying memory buffer when using
17
- * @see Source::newFromMemory()
18
- *
19
- * Must be freed when no longer needed.
20
- *
21
- * @internal
22
- */
23
- public ?\FFI \CData $ memory = null ;
24
-
25
15
public function __construct (\FFI \CData $ pointer )
26
16
{
27
17
$ this ->pointer = FFI ::vips ()->cast (FFI ::ctypes ('VipsSource ' ), $ pointer );
@@ -74,27 +64,19 @@ public static function newFromFile(string $filename): self
74
64
*/
75
65
public static function newFromMemory (string $ data ): self
76
66
{
77
- # we need to set the memory to a copy of the data
78
- $ n = strlen ($ data );
79
- $ memory = FFI ::vips ()->new ("char[ $ n] " , false , true );
80
- \FFI ::memcpy ($ memory , $ data , $ n );
81
- $ pointer = FFI ::vips ()->vips_source_new_from_memory ($ memory , $ n );
67
+ $ blob = FFI ::vips ()->vips_blob_copy ($ data , strlen ($ data ));
68
+ if ($ blob === null ) {
69
+ throw new Exception ("can't create source from memory " );
70
+ }
82
71
72
+ $ pointer = FFI ::vips ()->vips_source_new_from_blob ($ blob );
83
73
if ($ pointer === null ) {
84
- \ FFI ::free ( $ memory );
74
+ FFI ::vips ()-> vips_area_unref ( $ blob );
85
75
throw new Exception ("can't create source from memory " );
86
76
}
87
77
88
78
$ source = new self ($ pointer );
89
- $ source -> memory = $ memory ;
79
+ FFI :: vips ()-> vips_area_unref ( $ blob ) ;
90
80
return $ source ;
91
81
}
92
-
93
- public function __destruct ()
94
- {
95
- if ($ this ->memory !== null ) {
96
- \FFI ::free ($ this ->memory );
97
- }
98
- parent ::__destruct ();
99
- }
100
82
}
0 commit comments