自用 验证码 带一二三加减

<?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);
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值