Unicode图像在php imagettftext中被洗牌

时间:2017-12-06 14:47:31

标签: php unicode

我正在尝试使用GD库

在图像中打印unicode字符

我有以下代码

    $text = "கைவிடப்பட்ட";
    $font = "arial.ttf";
    $im = imagecreatetruecolor(2500, 500);
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 2500, 500, $white);
    imagettftext($im, 16, 0, 10, 20, $black, $font, $text);
    imagepng($im);
    imagedestroy($im);

这里的原文是கைவிடப்பட்ட,但打印如下,你可以看到第一个字母被替换。

demo

编辑:我尝试了5种不同的字体所有结果都相同(在线字体预览正确显示字符)。而当我通过php(从数据库)回显字符串到浏览器时,输出完全相同(கைவிடப்பட்ட),但为什么imagettftext正在切换字符..

2 个答案:

答案 0 :(得分:0)

Unicode的限制

Unicode标准策略是仅编码字符,而不编码字形。 கை,包含上的组合字符。泰米尔语被视为一个复杂的脚本,需要复杂的处理,许多软件组件都无法正确处理它。

最好的方法是找到或创建一个专为泰米尔语编写的php扩展库。

答案 1 :(得分:0)

我不确定GD库和环境,但我想这个问题是由于渲染造成的。 கை=க+ combination的组合,环境不支持显示时对unicode的渲染。所以要么检查任何渲染解决方案或使用非unicode字体或做以下解决方法来解决它在PHP中。

$text = "கைவிடப்பட்ட";
$imgtext = tamilrender($text);
$font = "arial.ttf";
$im = imagecreatetruecolor(2500, 500);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2500, 500, $white);
imagettftext($im, 16, 0, 10, 20, $black, $font, $imgtext);
imagepng($im);
imagedestroy($im);

function tamilrender($input)
{$input=  preg_replace("/([க-ஹ])([ைேெ])/u", "$2$1", $input);
$input= preg_replace("/([க-ஹ])ொ/u", "ெ$1ா", $input);
$input= preg_replace("/([க-ஹ])ோ/u", "ே$1ா", $input);
return $input;}
相关问题