获取PHP的imagefttext()以显示中文字符

时间:2017-06-01 10:54:40

标签: php image character-encoding

我想让imagefttext()显示中文字符,但它只显示方框

以下是一些显示问题的简化代码

<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);

// Path to our ttf font file
$font_file = 'C:\\Windows\\Fonts\\arial.ttf';

// Draw the text '牧師' using font size 13
imagefttext($im, 13, 0, 105, 55, $black, $font_file, '牧師');

// Output image to the browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

这就是它产生的:

enter image description here

是否与PHP中的字体或其他字符集设置有关?

我尝试了各种字体,但我似乎无法获得

  

牧师

要显示的字符

2 个答案:

答案 0 :(得分:1)

我设法通过使用SimSun字体来显示它们

更换

$font_file = 'C:\\Windows\\Fonts\\arial.ttf';

使用

$font_file = 'C:\\Windows\\Fonts\\simsun.ttc';

使其有效

答案 1 :(得分:0)

[请在您的字体文件夹下载字体,然后尝试,因为我已在本地服务器上完成此文本]

http://www.fontpalace.com/font-download/SimSun/ enter image description here