PHP 生成中文验证码乱码问题解决
//将要处理的汉字,先由UTF8的汉字转成html实体形式再使用.
代码如下
//示例代码:
$ChineseChar =
explode(',',"人,出,来,友,学,孝,仁,义,礼,廉,忠,国,中,易,白,者,火
,王,金,木,雷,风,龙,虎,天,地, 生,晕,菜,鸟,田,三,百,钱,福
,爱,情,兽,虫,鱼,九,网,新,度,哎,唉,啊,哦,仪,老,少,日, 月
,星,肯,各,程");
$k[0]=$ChineseChar[array_rand($ChineseChar)];
$k[1]=$ChineseChar[array_rand($ChineseChar)];
$k[2]=$ChineseChar[array_rand($ChineseChar)];
$k[3]=$ChineseChar[array_rand($ChineseChar)];
$cookie_str=implode('', $k);
setcookie('vrify',md5($cookie_str),time()+600);
$str[0]=iconv('gb2312','UTF-8',$k[0]);
$str[1]=iconv('gb2312','UTF-8',$k[1]);
$str[2]=iconv('gb2312','UTF-8',$k[2]);
$str[3]=iconv('gb2312','UTF-8',$k[3]);
//构造图像
$x_size=100;
$y_size=25;
$font='simhei.ttf';
$pic=imagecreate($x_size,$y_size);
$background_color = imagecolorallocate ($pic, 255, 255, 255);
$foregroundArr = array(imagecolorallocate($pic, rand(0, 20), rand(0,
20), rand(0, 20)),
imagecolorallocate($pic, rand(0,
20), rand(0, 10), rand(245, 255)),
imagecolorallocate($pic, rand(245,
255), rand(0, 20), rand(0, 10)),
imagecolorallocate($pic, rand(245,
255), rand(0, 20), rand(245, 255)));
imagettftext($pic,mt_rand(12,15),mt_rand(-8,8),6,mt_rand(19,22),$foregroundArr[rand(0,3)],$font,$str[0]);
imagettftext($pic,mt_rand(15,17),mt_rand(-8,8),26,20,$foregroundArr[rand(0,3)],$font,$str[1]);
imagettftext($pic,mt_rand(12,15),mt_rand(-8,8),46,20,$foregroundArr[rand(0,3)],$font,$str[2]);
imagettftext($pic,mt_rand(15,17),mt_rand(-8,8),66,mt_rand(19,22),$foregroundArr[rand(0,3)],$font,$str[3]);
for ($i=0;$i<80;$i++)
{
$pointcol = imagecolorallocate($pic, rand(0,255), rand(0,255),
rand(0,255));
imagesetpixel($pic,rand(2,$x_size-2),rand(2,$y_size-2),$pointcol);
}
$border = imagecolorallocate($pic, 133, 153, 193);
imagerectangle($pic, 0, 0, $x_size - 1, $y_size - 1,$border);
imagepng($pic);
imagedestroy($pic);
?>
代码如下
//ini_set('JIS-mapped Japanese Font Support',false);
$pic=imagecreate(250,30);
$black=imagecolorallocate($pic,0,0,0);
$white=imagecolorallocate($pic,255,255,255);
$font="C://WINDOWS//Fonts//simhei.ttf";
$str = '中华人民共和国';
$str = mb_convert_encoding($str, "html-entities","utf-8"
);//结果:哪一队优先开球
代码如下
代码如下
imagettftext($pic,10,0,10,20,$white,$font,$str);
<?php
session_start();
session_set_cookie_params(0,'/');
$enablegd = 1;
$funcs =
array('imagecreatetruecolor','imagecolorallocate','imagefill','imageline','imagedestroy','imagecolorallocatealpha','imageellips教程e','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
if(!function_exists('ob_gzhandler')) ob_clean();
header("Content-type: image/jpeg");
$filename='./photo.jpg';
$im=imagecreatefromjpeg($filename);
imagecopymerge($im,$pic,0,0,0,0,250,30,50);
imagejpeg($im);
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts,
mt_rand(0,strlen($consts)-1),1);
$vow[$x] = substr($vowels,
mt_rand(0,strlen($vowels)-1),1);
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1]
. $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4);
//only display 4 str
//set up image, the first number is the width and the second is
the height
$imageX = strlen($radomstring)*8; //the image width
$imageY = 20; //the
image height
$im = imagecreatetruecolor($imageX,$imageY);
//pChart 示例代码:
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180,
250), rand(180, 250));
$foregroundArr = array(imagecolorallocate($im, rand(0, 20),
rand(0, 20), rand(0, 20)),
imagecolorallocate($im,
rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im,
rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im,
rand(245, 255), rand(0, 20), rand(245, 255)));
$foreground2 = imagecolorallocatealpha($im, rand(20, 100),
rand(20, 100), rand(20, 100),80);
$middleground = imagecolorallocate($im, rand(200, 160), rand(200,
160), rand(200, 160));
$middleground2 = imagecolorallocatealpha($im, rand(180, 140),
rand(180, 140), rand(180, 140),80);
/*
Example14: A smooth flat pie graph
*/
//fill image with bgcolor
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//writes string
代码如下
本文由宝马娱乐在线城发布于互联网络,转载请注明出处:PHP 生成中文验证码乱码问题解决
关键词: