Setting Locale and Using PECL’s Imagick Library in PHP

I ran across an interesting issue today. An issue, that when I Googled it, appeared to have been resolved 2 years ago. However, I was using up-to-date versions of PHP, ImageMagick and the Imagick PECL library.

So, on to my issue. I have an abstract piece of code that when given the proper variables will set set the locale for my application. It sets the locale thusly:

setlocale(LC_ALL, 'fr_CA.utf8');

Later in the application, I was trying to draw a rectangle for a gauge that would graph a number against another number. When I tried to call the Imagick rectangle method, all hell broke loose.

$gauge = new ImagickDraw();
 
$x1 = $config->position->x1;
$y1 = $config->position->y1;
$x2 = $config->position->x2;
$y2 = $config->position->y2;
 
$gauge->rectangle($x1,$y1,$x2,$y2);

I then got this lovely message:

Fatal error: Uncaught exception 'ImagickException' with message 'Non-conforming drawing primitive definition `,' @ draw.c/DrawImage/3123'

Googled a bit and everything said this was fixed in the PECL library as of 2007. But it’s 2010 and I’m still having the issue!

Turns out if you set “LC_NUMERIC” to any French language locale, it can make interpreting floats difficult, because the French use commas (,) instead of periods (.) to delimit their fractions numerically. This caused the “rectangle” function of the “Imagick” PECL library to go crazy.

My Solution?

I altered my brash locale setting code to be a little more granular:

setlocale(LC_TIME, 'fr_CA.utf8');
setlocale(LC_MONETARY, 'fr_CA.utf8');

Setting locales for “LC_TIME” and “LC_MONETARY” was enough for me to get the programmatic localization I needed. And it made sure that “LC_NUMERIC” did not get set and cause localization on floats to interfere with external libraries.

This is “bang your head against the wall” stuff people. I hope this helps someone.

One Response to “Setting Locale and Using PECL’s Imagick Library in PHP”

  1. Tweets that mention su.percilio.us » Blog Archive » Setting Locale and Using PECL’s Imagick Library in PHP -- Topsy.com Says:

    [...] This post was mentioned on Twitter by Nick Bartkowiak. Nick Bartkowiak said: New blog post: http://su.percilio.us/2010/04/setting-locale-and-using-pecls-imagick-library-in-php/ [...]

Leave a Reply