Use cairo graphics library with PHP on CentOS

Install with yum

# yum install cairo-devel

Verify it works

with sample C program (taken from official documentation) (you can skip this and directly test it with PHP later)

$ mkdir cairotest
$ cd cairotest
$ vi hello.c

#include <cairo.h>

main (int argc, char *argv[])
        cairo_surface_t *surface =
            cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
        cairo_t *cr =
            cairo_create (surface);

        cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
        cairo_set_font_size (cr, 32.0);
        cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
        cairo_move_to (cr, 10.0, 50.0);
        cairo_show_text (cr, "Hello, world");

        cairo_destroy (cr);
        cairo_surface_write_to_png (surface, "hello.png");
        cairo_surface_destroy (surface);
        return 0;

$ export PKG_CONFIG_PATH="/usr/local/Cellar/glib/2.32.4/lib/pkgconfig:/usr/local/Cellar/cairo/1.12.2/lib/pkgconfig:/usr/local/Cellar/pixman/0.26.2/lib/pkgconfig:/usr/local/Cellar/libpng/1.5.12/lib/pkgconfig:/usr/X11/lib/pkgconfig"

$ cc -o hello $(pkg-config --cflags --libs cairo) hello.c

--> this should create hello.png image with "hello world" text in it

Cairo PHP integration

Usage / Resources

with pango

Installation On CentOS 6.3

Create a simple script that calls phpinfo() on any already working PHP-enabled website and verify that there isn't a section with title "cairo". Check the "Loaded Configuration File" entry an the beginning of the page. If it isn't /etc/php.ini, replace with the correct path below.

# yum install php-devel gcc make
# pecl install


Build process completed successfully
Installing '/usr/include/php/ext/cairo/php_cairo_api.h'
Installing '/usr/lib64/php/modules/'
install ok: channel://
configuration option "php_ini" is not set to php.ini location
You should add "" to php.ini

# echo "" >> /etc/php.ini
# service httpd restart

Verify that now there is a new section with title "cairo" in the phpinfo() output.


Adobe Illustrator message

Opening an EPS image created with cairo/PHP would show this message:

"f-0-0-Identity-H: Font not found on the system; missing font has been substituted."

This is a minor issue in the sense that this message can be safely ignored and the image can be edited normally, but the user won't be able to re-use the embedded font.

This issue won't arise when opening the EPS image with many other software intended for viewing/printing-only or rasterizing the image.

The only meaningful information that I found related to this message is in this thread

It seems to suggest that cairo was embedding the font converting it to postcript instead of using the original true type format.

Anyway, I found out that the offending PHP code was using

$cairo_context->showText( $string );

It was solved replacing it by:

$cairo_context->textPath ( $string );

But please, don't ask me why!

cairo (last edited 2014-05-21 16:01:17 by JaumeSola)