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

 2023-09-18 阅读 16 评论 0

摘要:闲着没事干写着玩的、wamp下测试没问题。服务器没有测试,使用请自行测试。代码如下随机验证码,/*** 验证码生成类php短信验证码api,* date 2014年7月26日 星期六*/session_start();class generateCode {private $codeLength = 5;//验证码长度private $height 

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

随机验证码,/**

* 验证码生成类

php短信验证码api,* 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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/75587.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息