您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了“php如何实现验证码类”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“php如何实现验证码类”这篇文章吧。
具体内容如下
<?php /** * * @author Administrator * */ class ValidateCode{ private $width; private $height; private $codeNum; private $img_resouce; private $disturbColorNum; private $checkCode; function __construct($width=80,$height=20,$codeNum=4) { $this->width=$width; $this->height=$height; $this->codeNum=$codeNum; $this->checkCode=$this->CreateCheckCode(); $number=floor($width*$height/25); if ($number>240-$codeNum) { $this->disturbColorNum=240-$codeNum; }else{ $this->disturbColorNum=$number; } } public function showImage($fontpath='') { //创建图像背景 $this->Img_resouce(); //var_dump($img_resouce); //设置干扰元素 $this->setDistructcolor(); //向图像中随机画出文本 $this->outputtext($fontpath); //输出图像 $this->outputimage(); } /** * *获取随机创建的验证码 */ public function getCheckCode(){ } private function Img_resouce(){ //创建一个真彩图像 $this->img_resouce=imagecreatetruecolor($this->width,$this->height); //随机设置图像背景 $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255)); //填充颜色 imagefill($this->img_resouce, 0, 0, $backcolor); //设置边框背景 $border=imagecolorallocate($this->img_resouce, 0,0,0); //画一个矩形 imagerectangle($this->img_resouce,0,0,$this->width-1,$this->height-1,$border); } private function setDistructcolor(){ //绘画干扰点 for ($i = 0; $i <$this->disturbColorNum; $i++) { imagesetpixel($this->img_resouce, rand(1, $this->width-2), rand(1, $this->height-2), rand(0,255)); } //绘画干扰线 for ($j = 0; $j <3; $j++) { $linecolor=imagecolorallocate($this->img_resouce,rand(0,255),rand(0,255),rand(0,255)); imagearc($this->img_resouce, rand(0,$this->width), rand(0,$this->height), rand(10, 225), rand(20, 150), 55, 44, $linecolor); } } private function CreateCheckCode(){ $code='23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ'; $string=''; for ($i = 0; $i < $this->codeNum; $i++) { $char=$code{rand(0, strlen($code)-1)}; $string.=$char; } return $string; } private function outputtext($fontpath=''){ for ($i = 0; $i < $this->codeNum; $i++) { $fontcolor=imagecolorallocate($this->img_resouce, rand(0,128), rand(0, 128), rand(0, 128)); if ($fontpath=='') { $fontsize=rand(3, 5); $x=floor($this->width/$this->codeNum)*$i+3; $y=rand(0, $this->height-20); imagechar($this->img_resouce, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor); }else{ $fontsize=rand(12, 16); $x=floor(($this->width-8)/$this->codeNum)*$i+8; $y=rand($fontsize, $this->height-15); imagettftext($this->img_resouce,$fontsize,rand(-45,45),$x,$y,$fontcolor,fontpath,$this->checkCode{$i}); } } } private function outputimage() { if (imagetypes() & IMG_GIF) { header("Content-type: image/gif"); imagegif($this->img_resouce); }else if(imagetypes() & IMG_JPEG) { header("Content-type: image/jpeg"); imagejpeg($this->img_resouce); }else if(imagetypes() & IMG_PNG) { header("Content-type: image/png"); imagepng($this->img_resouce); }else { echo "PHP不支持的类型"; } } private function __destruct(){ imagedestroy($this->img_resouce); } } ?>
以上是“php如何实现验证码类”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。