当前位置:Gxlcms > PHP教程 > phpGD库中文乱码的解决方法

phpGD库中文乱码的解决方法

时间:2021-07-01 10:21:17 帮助过:35人阅读

在php编程中,使用GD库时,有时会出现中文乱码的问题,本文给出一个解决方法,供大家学习参考。

注意:在用GD库输出中文字符串时,要使用imagettftext()函数,调用imagestring()函数是不行的。 参考示例如下。

示例1:

  1. <!--?php
  2. $pic=imagecreate(250,30);
  3. $black=imagecolorallocate($pic,0,0,0);
  4. $white=imagecolorallocate($pic,255,255,255);
  5. $font="C://WINDOWS//Fonts//simhei.ttf"; //必须是字符的路径
  6. $str ='php'.iconv('gb2312','utf-8','面对对象')." bbs.it-home.org";
  7. imagettftext($pic,10,0,10,20,$white,$font,$str);
  8. ?-->

示例2:文字水印。

  1. <!--?php
  2. /**
  3. * GD库应用 文字水印
  4. */
  5. $pic=imagecreate(250,30);
  6. $black=imagecolorallocate($pic,0,0,0);
  7. $white=imagecolorallocate($pic,255,255,255);
  8. $font="C://WINDOWS//Fonts//simhei.ttf";
  9. $str ='php'.iconv('gb2312','utf-8','面对对象')." bbs.it-home.org";
  10. imagettftext($pic,10,0,10,20,$white,$font,$str);
  11. header("Content-type: image/jpeg");
  12. $filename='../src/images/photo.jpg';
  13. $im=imagecreatefromjpeg($filename);
  14. imagecopymerge($im,$pic,0,0,0,0,250,30,50);
  15. imagejpeg($im);
  16. ?-->

人气教程排行