Getting started with PHP Cairo graphics extension on XP

Posted on November 6, 2010

2


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:

  1. Download fresh php binaries (http://php.net/downloads.php). I choose the 5.2.14 zip package.
  2. Unzip the downloaded php zip.
  3. 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!)
  4. Copying the php_cairo.dll from the downloaded cairo zip to the ext folder located insid the php directory.
  5. Editing the php.ini-recommended  filename (found in the php directory) to php.ini.
  6. Adding the line
    extension=ext/php_cairo.dll

    to the Windows Extensions part of php.ini (around line 664).

  7. 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:

>php thest_cairo.php

This should produce a test.png looking like this:

The example is taken from Micahel Macleans (http://mgdm.net/) talk on Graphics with Cairo - excellent read! Michael is one of the lead developers on the php cairo wrapper. Thank you, Michael!

UPDATE:

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/.

UPDATE 2:

Check out http://emsmith.net/cairo/cairo-0.1.0-alpha-tests.zip for php samples!

 

About these ads
Posted in: PHP, Uncategorized