PHP5 - 我想在图像上写一个印地语文本

时间:2011-05-10 06:29:10

标签: php image text utf-8

我想用PHP5在图像上写一个印地语文本(比如“पुलिसवालाआमिरखान”)。

我正在使用

$im = @imagecreate(210, 50);
$background_color = imagecolorallocate($im, 255, 255, 255); 
$text_color = imagecolorallocate($im, 0, 0, 0); 
imagestring($im, 7, 15, 15, utf8_encode("पुलिसवाला आमिर खान..."), $text_color);
ImagePNG($im, 'a.png'); 
imagedestroy($im);

3 个答案:

答案 0 :(得分:3)

使用函数imagettftext和印地文字体 字体路径应该是绝对的(使用realpath或只写绝对路径)。

答案 1 :(得分:2)

你肯定需要一个适用于Truetype字体的解决方案,imagestring()不会削减它。

  1. 获取包含您可以使用的印地语字符的TTF字体

  2. 摆脱utf8_encode(),只需确保文件是UTF8编码

  3. 使用imagefttext()代替imagestring()

答案 2 :(得分:1)

只要您的主机支持GD2和FreeType(与大多数服务器一样),您就可以使用imagettftext()将文本写入图像。您可以在此处找到其详细的语法和注释:

http://php.net/manual/en/function.imagettftext.php

查找支持印地语字符的字体(* .ttf或* .otf)。将字体文件放在与脚本相同的目录中,然后尝试使用此代码 - 将“yourhindifont.ttf”替换为您自己的字体文件名:

    <?php

    // your string, preferably read from a source elsewhere
    $utf8str = "पुलिसवाला आमिर खान";

    // buffer output in case there are errors
    ob_start();

    // create blank image
    $im = imagecreatetruecolor(400,40);
    $white = imagecolorallocate($im,255,255,255);
    imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);

    // write the text to image
    $font = "yourhindifont.ttf";
    $color = imagecolorallocatealpha($im, 50, 50, 50, 0); // dark gray
    $size = 20;
    $angle = 0;
    $x = 5;
    $y = imagesy($im) - 5;
    imagettftext($im, $size, $angle, $x, $y , $color, $font, $utf8str);

    // display the image, if no errors
    $err = ob_get_clean();
    if( !$err ) {
        header("Content-type: image/png");
        imagepng($im);
    } else {
        header("Content-type: text/html;charset=utf-8");
        echo $err;
    }

    ?>

如果您计划将Hindi直接输入到源中,请务必事先在编辑器中将源文档编码设置为UTF-8(无BOM)。否则,字符串将不会按预期存储。