4.10.06

My PHP Cairo extension

Few monthes ago I started developing my first PHP extension. I decided to wrap libCairo which is a very cool 2d vector graphics API used by GNOME and other major projects. When I started working on it I did some research on the net to see that no one else had started doing the same thing. I came across Pierre, a very known and respected person in the PHP community, who told me that he started working on on a huge extension which is supposed to become a full replacement for the good'ol GD extension, based on libCairo. I offered to join him since I already knew the libCairo API pretty well but he refused to share any code, claiming that his extension will be released very soon. A few weeks later I decided to go on and continue my work on libCairo Extension for PHP because: a. I couldn't find anything else I liked and b. I though it might even be extreamly useful as a simple wrapper for libCairo, at least until Pierre's extension is ready.

Few days ago I was doing some reading and came across another Cairo wrapper extension for PHP in development. So it seems like a duplicate effort being wasted, however, my implementation uses OOP API (similar to the python PHP extension) and the other extension is implemented in a flat, C-Style API.

I don't know whether my extension is really needed or whether it will be accepted to PECL so for now I'll wait with it until I will know that it will be useful for someone or maybe try to submit it to PECL and see what happens.

0.0.3 is the first public version of my libCairo extension and the source code is available for download.

The extension doesn't cover the whole Cairo API and there is no documentation but the following code will give you an idea of what the API looks like:




$Surface = new
CairoSurfaceImage(0,256,256);
$ctx = new CairoContext($Surface);
$ctx->setAntialias(0);
$ctx->setFillRule(0);
$ctx->scale(256/1.0, 256/1.0);
$pat = new CairoPatternGradientLinear(0.0, 0.0, 0.0, 1.0);
$pat->addColorStopRGBA(1, 0, 0, 0, 1);
$pat->addColorStopRGBA(0, 1, 1, 1, 1);
$ctx->rectangle(0,0,1,1);
$ctx->setSource($pat);
$ctx->fill();

$pat = new CairoPatternGradientRadial(0.45, 0.4, 0.1,
0.4, 0.4, 0.5);
$pat->addColorStopRGBA(0, 1, 1, 1, 1);
$pat->addColorStopRGBA(1, 0, 0, 0, 1);
$ctx->setSource($pat);
$ctx->arc(0.5, 0.5, 0.3, 0, 2*3.14);
$ctx->fill();
$Surface->toPng();
?>

The output of this will be:



This snippet fully work in the current version and will produce the above picture.

Anyway, I don't know what will become of my extension but if there will be demand for it i'll continue coding.

It was a very educational experience and I believe that I'm ready to get my fingers even deeper into the PHP engine.

5 comments:

Lafriks said...

Nice work! :) It works like a charm :) You just saved my day ;)

shlomil said...

Really??
But it's .. a little "half baked" ..
isn't it?

Anonymous said...

I think you are a respect man because you did saying the truth when you wrote about your extension .However, you can be easily better than him but on stages, How?.

From my opinion, I think you MUST still up to date with your extension and piece by piece in every release version your work will raise to the sky and in some day MAY be he will come to take your permit to share you in your extension and this is the state of our world (IT IS IN LOOP). That will come to you by HARE EFFORT, just look to your future target about your extension (DREAM HOW DOES LOOK LIKE?) after that MAY be you find a new message in your mail box that call you to be one of the PHP teamwork.

Enthuse your morale,

Abdullah

Anonymous said...

*HARD EFFORT

shlomil said...

Abdullah, Thank you for your comment.

This extension is now irrelevant.
There are two Cairo extensions in PECL:
http://pecl.php.net/package-search.php?pkg_name=cairo&bool=AND&submit=Search

One of them was developed during GSOC.

I lost interest in Cairo PHP.
A long time ago I had this vision that PHP can become a RAD env for desktop application. I don't think so anymore.