闲来无事,写了个带中文一、二、三的加减验证码,可以老是中文出错,找了要用iconv函数,但是我的页面编码本身就是UTF-8的,还请高手帮帮忙,闲着无聊的欢迎灌灌水。<?php 
session_start();
$_SESSION['code']= 0;
header("Content-type: image/png"); 
$im = @imagecreatetruecolor(70, 27) or die("建立图像失败"); //创建图片
$background_color = imagecolorallocate($im, 250, 250, 250); //图片添加背景
imagefill($im,0,0,$background_color);
$border_color = imagecolorallocate($im,0,0,0); //边框色
imagerectangle($im,0,0,69,26,$border_color);//噪音线
for($i = 0;$i < 7;$i++){
$x1 = rand(3,25);
$y1 = rand(2,26);//开始位置

$x2 = rand(45,68);//结束位置
$y2 = rand(2,26);
$line_color = imagecolorallocate($im,rand(190,255),rand(190,255),rand(190,255)); //噪音线颜色
imageline($im,$x1,$y1,$x2,$y2,$line_color); 
}
//显示的文字
$font_size = 18;
$first_num = 0;//图片中显示的数字,包含中文
$second_num = 0;$first_value = 0;//后台预算的数字
$second_value = 0;
$hanzi_num = array("一","二","三","四","五","六","七","八","九","十");//$str_HanZiOrNot = rand(0,1);//是否是汉字的判断
$result = 0;
$arrCapcher = array("+","-");
$capter = $arrCapcher[rand(0,1)];
switch ($capter){
case "+":
//产生第一个数字
if(rand(0,1) == 0){// ==0表示纯数字
$first_num = rand(2,10);
$first_value = $first_num;
}
else{       //汉字
$index_ = rand(0,10);
$first_num = $hanzi_num[$index_];
$first_value = $index_ + 1;
}
//产生第二个数字
if(rand(0,1) == 0){//      ==0纯数字
$second_num = rand(0,10);
$second_value = $second_value;
}
else{
$index_ = rand(0,10);
$second_num = $hanzi_num[$index_];
$second_value = $index_ + 1;
}

$result = $first_value + $second_value;
break;
case "-":
//产生第一个数字
if(rand(0,1) == 0){// ==0表示纯数字
$first_num = rand(2,10);
$first_value = $first_num;
}
else{       //汉字
$index_ = rand(0,10);
$first_num = $hanzi_num[$index_];
$first_value = $index_ + 1;
}
//产生第二个数字
if(rand(0,1) == 0){//      ==0纯数字
$second_num = rand(0,$first_value);
$second_value = $second_value;
}
else{
$index_ = rand(0,$first_value-1);
$second_num = $hanzi_num[$index_];
$second_value = $index_ + 1;
}

$result = $first_value - $second_value;
break;
}$x = rand(2,5);
$arr_X_Y = array(array($x,rand(1,3)),array($x+$font_size,rand(1,4)),array($x+2*$font_size,rand(1,3)),array($x+3*$font_size,rand(1,4)));
$str_char = array($first_num,$capter,$second_num,"=");for($i = 0;$i< 4;$i++){
$text_color = imagecolorallocate($im,rand(50,120),rand(50,120),rand(50,120)); 
imagechar($im,$font_size,$arr_X_Y[$i][0],$arr_X_Y[$i][1],$str_char[$i],$text_color); 
}$_SESSION['code'] = $result;
imagepng($im); 
imagedestroy($im);
?>
验证码IM