Home  >  Article  >  Backend Development  >  Solution to Chinese garbled characters in php GD library

Solution to Chinese garbled characters in php GD library

WBOY
WBOYOriginal
2016-07-25 09:00:301208browse
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);
?>

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);
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn