In PHP programming, when using the GD library, the problem of Chinese garbled characters sometimes occurs. This article gives a solution for your reference.
Note: When using the GD library to output Chinese strings, you must use the imagettftext() function. Calling the imagestring() function will not work. Reference examples are as follows. Example 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); ?> Copy after login Example 2: Text watermark. <?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); ?> Copy after login |