php gd garbled solution: 1. Convert the encoding through "iconv('gb2312','utf-8','')"; 2. Call the imagettftext() function to output the Chinese string.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php gd is garbled?
Solution to the problem of GD Chinese garbled characters:
Today I carefully studied some related technologies of GD, and also studied the problem of GD Chinese garbled characters.
Using the GD library to output Chinese strings, calling imagestring is useless. You need to use the imagettftext() function. Please refer to the manual for the specific use of the imagettftext function.
Here is a usage example:
$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','面对对象')." www.phpobject.net"; imagettftext($pic,10,0,10,20,$white,$font,$str);
I gave a simple GD watermark example earlier, and only gave an example of how to use pictures to watermark Yes, here is a simple code for text watermark.
<?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','面对对象')." www.phpobject.net"; 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); ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What to do if php gd is garbled?. For more information, please follow other related articles on the PHP Chinese website!