加乘验证码改进

好吧,这是我博客园上的,真不知道是转载还是原创!原始地址:http://www.cnblogs.com/cntnn11/archive/2012/09/03/2669145.html

不过,CSDN的编辑器比博客园的好看多了大笑

下午活少,又刚看php的GD库不久,写个验证码练练手。

没做什么修饰,一路直下,no function,no class!

里边的函数查手册吧,完全不会的先去把php的GD库看一遍。

还是不发首页。

这是效果图:



<?php
session_start();
/**
 *	一个简单的加乘验证码类
 *	@author 谭宁宁
 *	@datetime 2012-09-03
*/

class codeimg
{
	public $width	= 150;
	public $height	= 35;
	
	public $num_1	= 1;
	public $num_2	= 10;
	
	public $f_file	= 'font/msyhbd.ttf';
	public $f_size	= 13;

	public $c_type	= 1;	//验证码类型:1加乘混合出现/2纯加法/3纯乘法。默认为1
	public $imgSourc= '';

	public $line	= true;
	public $lineNum	= 0;

	private $sess	= '';	//存入session的值
	private $dev	= FALSE;//调试模式,也就是不输出header("content-type: image/png;");也不生成图片。测试的

	function __construct()
	{
		$this->genImg();
	}

	function genImg()
	{
		$this->imgSourc	= $this->createImgSourc();
		if(!self::$this->dev)
		{
			header("content-type: image/png;");
			imagepng($this->imgSourc);
			imagedestroy($this->imgSourc);
		}
	}

	function createImgSourc()
	{
		$this->imgSourc	= imagecreate($this->width, $this->height);
		$bgcolor	= imagecolorallocate($this->imgSourc, 255, 255, 255);

		//是否生成干扰线
		if($this->line)
		{
			$this->genLine();
		}

		$text	= $this->genStr();
		$strlen	= mb_strlen($text);

		$y		= intval(($this->height-$this->f_size));
		$x		= intval(($this->height-$strlen));

		$textColor	= imagecolorallocate($this->imgSourc, rand(0, 125), rand(0, 125), rand(0, 125));
		imagettftext($this->imgSourc, $this->f_size, 0, $x, $y, $textColor, $this->f_file, $text);

		return $this->imgSourc;
	}

	/**
	 *	干扰线线的生成
	 *	根据$this->lineNum决定生成多少条线,默认为随机生成5~12条
	*/
	function genLine()
	{
		$lineNum	= empty($this->lineNum) ? rand(5, 12) : $this->lineNum;
		for($i = 1; $i <= $lineNum; $i++ )
		{
			$linecolor	= imagecolorallocate($this->imgSourc, rand(0 , 255), rand(0, 255), rand(0, 255));
			imageline($this->imgSourc, rand(1 , $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $linecolor);
		}
	}

	/**
	 *	生成图片里的内容
	 *	@return string
	 */
	function genStr()
	{
		$operArr	= array('加', '乘');
		$operNum	= $this->c_type === 2 ? 0 : ($this->c_type === 3 ? 1 : rand(0, 1));

		$numArr[0]	= rand(intval($this->num_1), intval($this->num_2));
		$numArr[1]	= rand(intval($this->num_1), intval($this->num_2));

		if($operNum === 1)
		{
			$numArr[2]	= intval($numArr[0])*intval($numArr[1]);
		}
		else
		{
			$numArr[2]	= intval($numArr[0])+intval($numArr[1]);
		}

		$temp_num	= rand(0, 2);
		self::$this->sess	= $numArr[$temp_num];
		$numArr[$temp_num]	= '**';
		$string		= $numArr[0].' '.$operArr[$operNum].' '.$numArr[1].' = '.$numArr[2];
		return $string;
	}

	function getValue()
	{
		return self::$this->sess;
	}

}
header("content-type: text/html; charset=utf-8");
$objimg	= new codeimg();
$_SESSION['scode']	= $objimg->getValue();
?>




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值