在php编程中,使用GD库时,有时会出现中文乱码的问题,本文给出一个解决方法,供大家学习参考。
注意:在用GD库输出中文字符串时,要使用imagettftext()函数,调用imagestring()函数是不行的。
参考示例如下。
示例1:
- <!--?php
- $pic=imagecreate(250,30);
- $black=imagecolorallocate($pic,0,0,0);
- $white=imagecolorallocate($pic,255,255,255);
- $font="C://WINDOWS//Fonts//simhei.ttf"; //必须是字符的路径
- $str ='php'.iconv('gb2312','utf-8','面对对象')." bbs.it-home.org";
- imagettftext($pic,10,0,10,20,$white,$font,$str);
- ?-->
示例2:文字水印。
- <!--?php
- /**
- * GD库应用 文字水印
- */
- $pic=imagecreate(250,30);
- $black=imagecolorallocate($pic,0,0,0);
- $white=imagecolorallocate($pic,255,255,255);
- $font="C://WINDOWS//Fonts//simhei.ttf";
- $str ='php'.iconv('gb2312','utf-8','面对对象')." bbs.it-home.org";
- imagettftext($pic,10,0,10,20,$white,$font,$str);
- header("Content-type: image/jpeg");
- $filename='../src/images/photo.jpg';
- $im=imagecreatefromjpeg($filename);
- imagecopymerge($im,$pic,0,0,0,0,250,30,50);
- imagejpeg($im);
- ?-->
|