php输出中文字符

时间:2023-03-10 05:57:08
php输出中文字符

中文字符不可以使用imagettftext()函数在图片中直接输出,如果要输出中文字符,需要先使用iconv()函数对中文字符进行编码,语法格式如下:
string iconv ( string $in_charset, string $out_charset, string $str )
说明:参数$in_charset是中文字符原来的字符集,$out_charset是编码后的字符集,$str是需要转换的中文字符串。函数最后返回编码后的字符串。这时使用imagettftext()函数就可以在图片中输出中文了。例如:
<?php
header("Content-type: image/gif");
$image=imagecreate(200, 200); //创建图形
$background=imagecolorallocate($image, 255, 255, 255); //背景色为白色
$red=imagecolorallocate($image, 0, 255, 0); //定义绿色
$text='南京2014青奥会'; //初始化字符串
$font='C:\WINDOWS\Fonts\simhei.ttf'; //字体文件,黑体
$codetext=iconv("UTF-8", "UTF-8", $text); //对中文进行编码
imagettftext($image, 20, 0, 10, 150, $red, $font, $codetext); //水平输出字符串$codetext
imagegif($image); //输出图形
imagedestroy($image);
?>