php实现数字英文验证码,PHP英文数字验证码生成类

闲着没事干写着玩的、wamp下测试没问题。服务器没有测试,使用请自行测试。代码如下

/**

* 验证码生成类

* date 2014年7月26日 星期六

*/

session_start();

class generateCode {

private $codeLength = 5;//验证码长度

private $height = 70; //画布高度

private $width = 25; //画布宽度

private $fontSize = 10;

private $noisenum=100;//干扰点数量

private $code = null;

/**

* @param int $width 图片宽度

* @param int $height图片高度

* @param int $fontSize 字体大小

* @param int $codeLength 验证码长度

*/

public function __construct($width,$height,$fontSize,$codeLength){

$this->width = $width; //图像宽度

$this->height = $height;

$this->fontSize = $fontSize;

$this->codeLength=$codeLength;

$this->code = $this->getCode();

$_SESSION['code'] = $this->code;

}

public function generate(){

$font = 'Weston.otf';

$img = imagecreate($this->width,$this->height);

$black = ImageColorAllocate($img, 0,0,0); //RGB黑色标识符

$white = ImageColorAllocate($img, 255,255,255); //RGB白色标识符

$gray = ImageColorAllocate($img, 200,200,200); //RGB灰色标识符

imagettftext($img, $this->fontSize, 0, 10, 20, $gray, $font, $this->code);

//干扰线

$this->setnoise($img,$this->width,$this->height,$this->noisenum);

header ( 'Content-type: image/png' );

imagepng($img);

imagedestroy($img);

}

/**

* 随机生成验证码字符

* @param int $codeLength //生成随机字符长度

* return $key 返回生成随机字符

*/

protected function getCode(){

$codeStr = '23456789ABCDEFGHIJKLOMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz';//字符池

$strLeng = strlen($codeStr);

for($i=0;$icodeLength;$i++){

$key.=$codeStr{mt_rand(1,$strLeng)};

}

return $key;

}

/**

* 添加干扰点

* @param object $image 图片对象

* @param int $width 图片宽度

* @param int $height 图片高度

* $noisenum 生成点数量

*/

protected function setnoise($image,$width,$height,$noisenum){

for ($i=0; $i

//分配颜色

$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));

//画点

imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);

//画线

if($i<3)

imageline($image,rand(0, $width),rand(0, $height),rand(0, $width),rand(0, $height),$randColor);

}

}

}

$code = new generateCode(100,30,15,5);

$code->generate();

效果

f0d824605b295f5c7a502e07a172836c.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值