登录

php - ImageString如何把中文字符输出到图片中?

试过利用iconv把中文字符转码然后输出,但还是乱码

    $s="你若输出,便是晴天";
    $ss=iconv("gb2312","utf8",$s);

    $im=ImageCreate(400,400);
    $black=ImageColorAllocate($im,0x00,0x00,0x00);
    ImageString($im,5,200,200,$ss,$black);
    Header('Content-Type:image/png');
    ImagePNG($im);

在网上搜了好久也没找到解决方法。 这个怎么破呢?

# PHP
高洛峰高洛峰2147 天前491 次浏览

全部回复(2) 我要回复

  • 高洛峰

    高洛峰2017-04-10 14:35:43

    imagestring使用点阵字库,如果你真的要用他的话需要自己构造中文的点阵字库,然后用imageloadfont来载入。
    更推荐和常用的做法是使用imagettftext函数

    <?php
    header("Content-type: image/jpeg");
    $im = imagecreatetruecolor(100,50);
    $textcolor = imagecolorallocate($im,255,255,255);
    $font = "C://windows/fonts/simhei.ttf";
    $text='中国';
    $text = mb_convert_encoding($text,'UTF-8','GB2312');
    imagettftext($im,10,4,20,20,$textcolor,$font,$text);
    imagejpeg($im);
    ?>
    

    回复
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 14:35:43

    输出中文最好使用 imagettftext()

    imagestring基本无能为力

    回复
    0
  • 取消回复发送