The following is the phpVerification codeencapsulation class I compiled. If you are interested, you may wish to take a look
length=$length; $this->font=$font; } //生成随机字符串 private function generalCode() { $char_array=array_merge(range('A','Z'),range('a','z'),range(0,9));//生成一个字母和数字的数组 $index=array_rand($char_array,$this->length); //随机取4个字符,返回的是字符下标 shuffle($index); //打乱下标 //拼接字符串 $str=''; foreach($index as $i) { $str.=$char_array[$i]; } return $str; } //生成验证码 public function generalCaptcha() { $str=$this->generalCode(); //打开背景图 $bg_path='./captcha/captcha_bg'.rand(1,5).'.jpg'; //背景图地址 5张随机背景图片 $img=imagecreatefromjpeg($bg_path); //打开图片 //定义前景色 $color=imagecolorallocate($img,0,0,0); if(rand(1,2)==2) $color=imagecolorallocate($img,255,255,255); //将字符串写到图片上 $x=(imagesx($img)-imagefontwidth($this->font)*strlen($str))/2; $y=(imagesy($img)-imagefontheight($this->font))/2; imagestring($img,$this->font,$x,$y,$str,$color); header('content-type:image/png'); imagepng($img); imagedestroy($img); } } //测试 $captcha=new CaptchaLib(8,4); $captcha->generalCaptcha();
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Key points analysis and answers to rewriting and polymorphism
How to rewrite the padleft method in js
What are the inheritance methods in js
The above is the detailed content of php verification code encapsulation class. For more information, please follow other related articles on the PHP Chinese website!