<?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,25);//开始位置
$x2 = rand(45,68);//结束位置
$y2 = rand(2,25);
$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,9);
$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,9);
$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(3,10);
$first_value = $first_num;
}
else{ //汉字
$index_ = rand(0,9);
$first_num = $hanzi_num[$index_];
$first_value = $index_ + 1;
}
//产生第二个数字
if(rand(0,1) == 0){// ==0纯数字
$second_num = rand(0,$first_value-1);
$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,"=","?");
$strs = "";//文字字符串
foreach ($str_char as $value){
$strs .= $value;
}
$text_color = imagecolorallocate($im,rand(50,120),rand(50,120),rand(50,120));
imagettftext($im,15,0,1,20,$text_color,"font/a.ttf",$strs); //输入汉字时必须使用汉字的字体font
$_SESSION['code'] = $result;
imagepng($im);
imagedestroy($im);
?>
自用 验证码 带一二三加减
最新推荐文章于 2023-12-26 14:34:36 发布