搜索了一下,說是由於GD庫編譯時添加了: –enable-gd-jis-conv 的支持( GD: Enable JIS-mapped Japanese font ) , 在phpinfo()裡也可以看到:JIS-mapped Japanese Font Support的支持為enable
取消這個日文支持,亂碼即可消除。
另外不用重新編譯也可以
將字符串轉碼一次也可以
$str = mb_convert_encoding($str, "html-entities","utf-8" );//
<?php
session_start();
$im = imagecreatetruecolor(80,30);
$bg = imagecolorallocate($im,0,0,0);
$te = imagecolorallocate($im,255,255,255);
for($i=0;$i<5;$i++)
{
$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,80),0,80,rand(0,30),$te2);
}
for($i=0;$i<200;$i++)
{
imagesetpixel($im,rand()%100,rand()%30,$te2);
}
srand((float) microtime() * 10000000);
$input = array("步","步","高","請","轉","到","圖","像","我","非","常","高","興");
$rand_keys = array_rand($input, 4);
$_SESSION['yzm'] = $t = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]];
//$t =iconv("GB2312","UTF-8",$t);//
$t = mb_convert_encoding($t, "html-entities","utf-8" );//
imagettftext($im,12,0,10,20,$te,'simhei.ttf',$t);
header("content-type: image/jpeg");
imagejpeg($im);
?>