问题
I have a web site that runs on Windows and uses cp1252
(aka Win-1252
) so it can display Spanish characters. The app generates some plots with JpGraph 2.3. These plots use the Tahoma Open Type font family to display text labels. Strings are provided in ANSI (i.e., cp1252) and font files support cp1252 (actually, the *.ttf files were copied from the system's font folder).
It's been working fine in several setups from PHP/5.2.6 to PHP/5.3.0. Problems started when I ran the app under PHP/5.3.1: all non-ASCII are replaced by the hollow rectangle that represents missing or unknown chars.
JpGraph's documentation is not very precise about how it expects international chars. Apparently, text is handled internally by the imagettftext() function, which expects UTF-8. However, encoding everything as UTF-8 breaks the app in all the systems. Where ANSI used to work fine, I get wrong characters (Ê instead of Ú). Where I got missing chars, now I get a PHP error:
Warning: imagettftext(): any2eucjp(): something happen
Do you have any clue about what changed in GD2 from PHP/5.3.0 to 5.3.1 that could be affecting the rendering on non-ASCII chars? How am I expected to feed JpGraph with strings in the Win-1252 charset?
Update
Definitively, the issue is not related to JpGraph. I can reproduce the issue with a simple call to imagettftext():
<?php
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
//$text = 'áéíóú ÁÉÍÓÚ'; # ANSI
$text = utf8_encode('áéíóú ÁÉÍÓÚ'); # UTF8
$font = '/path/to/tahomabd.ttf';
imagettftext($im, 15, 0, 10, 25, $black, $font, $text);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
In PHP/5.3.0, both ANSI and UTF-8 render the right text. In PHP/5.3.1, ANSI renders wrong characters and UTF-8 triggers the any2eucjp(): something happen
internal error. I'd dare say I've hit a bug...
回答1:
A summary of my findings:
- It's probably a bug in PHP/5.3.1 (or in my PHP/5.3.1 binary build); PHP/5.3.2 no longer misses chars or throws a warning.
- A workaround is to encode non-ASCII chars with numeric HTML entities.
来源:https://stackoverflow.com/questions/2666105/missing-chars-in-jpgraph