php 生成数学运算验证码图片

1 篇文章 0 订阅
1 篇文章 0 订阅

php使用gd扩展生成数学运算验证码

以下是php代码,纯原创,珍惜作者的作品

class Verifyjiaqiang{
	
	public $width;
	public $height;
	public $size;
	public $code;
	public $result;
	public function __construct($config=array())
	{
		$this->size =  empty($config['size']) ? 20 : $config['size'];   //验证码尺寸
		$this->width = empty($config['width']) ? 80 : $config['width'];  //验证码图片宽度
		$this->height = empty($config['height']) ? 30 : $config['height'];  //验证码图片高度
		$this->code = $this->get_code();
		
	}
	
	/*
	 * @desc 生成验证码
	 */
	public function get_code()
	{
        $shuzi = [
            0=>'零',
            1=>'一',
            2=>'二',
            3=>'三',
            4=>'四',
            5=>'五',
            6=>'六',
            7=>'七',
            8=>'八',
            9=>'九',
            10=>'十',
            11=>'0',
            12=>'1',
            13=>'2',
            14=>'3',
            15=>'4',
            16=>'5',
            17=>'6',
            18=>'7',
            19=>'8',
            20=>'9',
            21=>'10',
            22=>'壹',
            23=>'②',
            24=>'叁',
            25=>'④',
            26=>'伍',
            27=>'⑥',
            28=>'柒',
            29=>'⑧',
            30=>'玖',
            31=>'⑩',
        ];
        $fuhao  = [0=>"加",1=>"减",2=>"乘",3=>"除以",4=>"+",5=>"-",6=>"*",7=>"/",8=>"÷",9=>"×"];
        $guanxi = [
            "零"=>"0",
            "一"=>"1",
            "二"=>"2",
            "三"=>"3",
            "四"=>"4",
            "五"=>"5",
            "六"=>"6",
            "七"=>"7",
            "八"=>"8",
            "九"=>"9",
            "十"=>"10",
            "加"=>"+",
            "减"=>"-",
            "乘"=>"*",
            "除以"=>"/",
            "+"=>"+",
            "-"=>"-",
            "*"=>"*",
            "/"=>"/",
            "÷"=>"/",
            "×"=>"*",
            "壹"   => "1",
            "②"   => "2",
            "叁"   => "3",
            "④"   => "4",
            "伍"   => "5",
            "⑥"   => "6",
            "柒"   => "7",
            "⑧"   => "8",
            "玖"   => "9",
            "⑩"   => "10",
            "0"   => "0",
            "1"   => "1",
            "2"   => "2",
            "3"   => "3",
            "4"   => "4",
            "5"   => "5",
            "6"   => "6",
            "7"   => "7",
            "8"   => "8",
            "9"   => "9",
            "10"   => "10",
        ];

        $yunxuanfuhao = $fuhao[array_rand($fuhao,1)];
        $yunsuan1 = $shuzi[array_rand($shuzi,1)];
        $yunsuan2 = $shuzi[array_rand($shuzi,1)];

        if($guanxi[$yunxuanfuhao] === "/") {
            while ($guanxi[$yunsuan2]==0){
                $yunsuan2 = $shuzi[array_rand($shuzi,1)];
                if ($guanxi[$yunsuan2]>0) {
                    break;
                }
            }
            eval("\$jieguo=round($guanxi[$yunsuan1]$guanxi[$yunxuanfuhao]$guanxi[$yunsuan2],1);");
        } else if ($guanxi[$yunxuanfuhao] === "-") {
            while ($guanxi[$yunsuan2]>$guanxi[$yunsuan1]){
                $yunsuan1 = $shuzi[array_rand($shuzi,1)];
                $yunsuan2 = $shuzi[array_rand($shuzi,1)];
                if ($guanxi[$yunsuan2]<$guanxi[$yunsuan1]) {
                    break;
                }
            }
            eval("\$jieguo=$guanxi[$yunsuan1]$guanxi[$yunxuanfuhao]$guanxi[$yunsuan2];");
        }else {
            eval("\$jieguo=$guanxi[$yunsuan1]$guanxi[$yunxuanfuhao]$guanxi[$yunsuan2];");
        }
        $this->result = $jieguo;
        $code = $yunsuan1  . $yunxuanfuhao . $yunsuan2;
        return mb_convert_encoding($code, "UTF-8","auto");
	}
	
	/*
	 * @desc 生成验证码
	 */
	public function create_verify()
	{
		// 画图像
		$im = imagecreatetruecolor($this->width, $this->height);

		// 定义要用到的颜色
	    $back_color = imagecolorallocate($im, 235, 236, 237);
	    $boer_color = imagecolorallocate($im, 118, 151, 199);
	    $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));

	    // 画背景
	    imagefilledrectangle($im, 0, 0, $this->width, $this->height, $back_color);

	    // 画边框
	    imagerectangle($im, 0, 0, $this->width-1, $this->height-1, $boer_color);
			    // 画干扰线
	    for($i = 0;$i < 5;$i++) 
	    {
	        $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	        imagearc($im, mt_rand(- $this->width, $this->width), mt_rand(- $this->height, $this->height), mt_rand(30, $this->width * 2), mt_rand(20, $this->height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
		}
	    
	    // 画干扰点
	    for($i = 0;$i < 50;$i++) 
	    {
	        $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	        imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $font_color);
		}

	    // 画验证码$font = './public/fonts/FreeSerifItalic.ttf';
	    $filepath = realpath("simhei.ttf");
	    imagefttext($im, $this->size , 0, 5, $this->size + 3, $text_color, $filepath, $this->code);
		   

	    header("Content-type: image/png;charset=utf-8");
	    imagepng($im);
	    imagedestroy($im);
	}
}

总结:
如果需要增加随机数,在变量$shuzi中增加字段,增加完之后还需要在变量$guanxi中增加对应关系。
如果验证码汉字出现问题,需要排查文件编码问题,或者是字体库的问题。
如果字体库找不到,要是用绝对路径,php 函数 realpath()

使用方法:

 		$this->load->library('verifyjiaqiang');
        $code = $this->verifyjiaqiang->result;  //这个是计算结果,保存结果即可
        $this->load->library('session');
        $this->session->set_userdata('yykpinlun2',$code);
        $this->verifyjiaqiang->create_verify();

效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值