The Cairo graphics library (http://cairographics.org/) is a widely used solution for rendering graphics to different devices – bitmaps, pdf, svg etc. It’s used by Mozilla, Inkscape, Gimp etc. Some time ago, I realized that Cairo is available as a native php extension: http://php.net/manual/en/book.cairo.php. Great!
To get it running on XP, I did the following:
- Download fresh php binaries (http://php.net/downloads.php). I choose the 5.2.14 zip package.
- Unzip the downloaded php zip.
- Download and unzip the the cairo-0.1.0-alpha-php-5.2.10.zip, conatining the cairo wrapper extension (php_cairo.dll). It can be found using the PHP 5.2 link available here:
http://perisama.net/cairo/UPDATE: http://www.emsmith.net/cairo/ (Thank you, Jaffer Haider for pointing out this url!)
- Copying the php_cairo.dll from the downloaded cairo zip to the ext folder located insid the php directory.
- Editing the php.ini-recommended filename (found in the php directory) to php.ini.
- Adding the line
to the Windows Extensions part of php.ini (around line 664).
- This should be it!
Now, it should be possible to run php from the command line with cairo commands.
For example, create a file named “test_cairo.php” with the following content and put it in the php directory:
<?php // test_cairo.php $s = new CairoImageSurface(CairoFormat::ARGB32, 400, 400); $c = new CairoContext($s); $c->fill(); $c->setSourceRGB(1, 0, 0); $c->setLineWidth(50); $c->arc(200, 200, 100, 0, 2 * M_PI); $c->stroke(); $c->setSourceRGB(0, 0, 0.6); $c->rectangle(0, 160, 400, 75); $c->fill(); $s->writeToPng(dirname(__FILE__) . '/test.png');
Now, run open a command line window in the php directory and run the testfile like this:
This should produce a test.png looking like this:
If you want to use the cairo extension with PHP 5.2, Eclipse PDT and Zend debugging client (http://www.thierryb.net/pdtwiki/index.php?title=Using_PDT_:_Installation_:_Installing_the_Zend_Debugger), make sure to download the Non Thread Safe (PHP 5.2 NTS) from http://perisama.net/cairo/.
Check out http://emsmith.net/cairo/cairo-0.1.0-alpha-tests.zip for php samples!