Reputation: 856
My goal is to create a PHP extension for a proprietary image format, and have it create a gd-compatible image resource, that can be used with the gd PHP extension's functions.
Now, browsing through the gd source code, I came across the struct/typedef for their gdImagePtr
pointer - which appears to be what the extension wraps as the aforementioned "image resource". The struct itself consists entirely of standard C types, so that leads me to ask...
If I were to use that same struct in my extension, fill it with the appropriate data from this image format, and make it available to PHP in the same way, would that work? Or does this image resource act more like a handle, and must be created within gd itself for it to work?
Upvotes: 0
Views: 80
Reputation: 856
The answer is yes, with one small extra step.
My first test was to simply interact with gd directly - and sure enough, as long as the struct is filled properly, gd is more than happy to use a third party gdImagePtr for other uses. In my test, I simply created a form with a picture box, and used .NET's unmanaged memorystream combined with gdImagePngPtr
, feeding it my filled gdImagePtr
, and it worked perfectly.
The next step, of course, was to do this with PHP, but since there's a middle-man - namely the ZEND_RESOURCE that this gdImagePtr
gets turned into, there's one extra step involved. ZEND gives each extension its own unique id - so simply creating your own resource, with that struct, isn't going to work. But, the gd extension provides its own API - with only one function.
So if you include php_gd.h
, you can do the following:
ZEND_REGISTER_RESOURCE(return_value, im, phpi_get_le_gd());
replacing the standard module_number
(your extension's id) with phpi_get_le_gd()
will make that resource "belong" to the gd extension, allowing you to use the image resource with other gd functions in PHP.
Upvotes: 1