1.配置文件config.php
1 <?php 2 3 成都职业高中 /** 4 * 验证码类型 codetype int 0:纯数字 1:纯字符串 2:数字和字符串混合 5 * 验证码长度 length int 6 * 图片宽度 width int 7 * 图片高度 height int 8 */ 9 return10 [11 'codetype' => 2,12 'length' => 5,13 'width' => 400,怎么夸别人14 'height' => 200,15 ];
2.生成验证码类
<?php //定义验证码类 class code{ private $length; //验证码长度 private $codetype; //类型 private $code; //验证码 private $width; //宽度 private $height; //高度 private $img; //图片资源 public function __construct() { //引入配置文件 $this->config = require_once './config.php'; $this->length = $this->config['length']; $this->codetype = $this->config['codetype']; $this->width = $this->config['width']; $this->height = $this->config['height']; $this->createcode(); } protected function createcode() { switch($this->codetype){ ca 0: $this->code = $this->getnumbercode(); break; ca 1: $this->code = $this->getcharcode(); break; ca 2: $this->code = $this->getnumcharcode(); break; default: die('验证码类型错误,请重新输入!'); break; } } public function getcode() { return $this->code; } private function getnumbercode() { $number = join('',range(0,9)); return $this->tcode($number); } private function getcharcode() { $str = join('',range('a','z')); $str .= strtoupper($str); return $this->tcode($str); } private function getnumcharcode() { $number = join('',range(0,9)); $str = join('',ran大语ge('a','z')); $code = strtoupper($str).$str.$number; return $this->tcode($code); } private function tcode($string) { return substr(str_shuffle($string),0,$this->length); } //输出图像 public function getimg() { //新建画布 $this->createimg(); //画布填充背景色 $this->fillbackground(); //将验证码写入画布 $thi中国医科大学在哪s->fillcode(); //加入干扰点 $this->tdistubpoint(); //设置干扰线 $this->tdiarc(); //显示图像 $this->showimg(); } protected function createimg() { 我的妹妹有点怪 $this->img = imagecreatetruecolor($this->width,$this->height); } protected function fillbackground() { imagefill($this->img,0,0,$this->lightcolor()); } private function lightcolor() { return imagecolorallocate($this->img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255)); } private function darkcolor() { return imagecolorallocate($this->img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); } protected function fillcode() { $width = ceil($this->width/$this->length); $height = $this->height/2; for($i=0;$i<$this->length;$i++){ $x = mt_rand($i*$width+10,($i+1)*$width-10); $y = mt_rand($height-10,$height+10); imagechar($this->img,5,$x,$y,$this->code[$i],$this->darkcolor()); } } //设置干扰点 protected function tdistubpoint() { for($i=0;$i<1000;$i++){ $x = mt_rand(0,$this->width); $y = mt_rand(0,$this->height); imagetpixel($this->img,$x,$y,$this->darkcolor()); } } //设置干扰线段 protected function tdiarc() { for($i=0;$i<2;$i++){ imagearc ( $this->img , rand(0,$this->width) , rand(0,$this->height) , rand($this->width,$this->width*2) , rand($this->height,$this->height*2) , rand(0,100) , rand(280,270) , $this->darkcolor() ); } } protected function showimg() { header('content-type:image/png'); imagepng($this->img); } } $code = new code(); $code ->getimg();
本文发布于:2023-04-08 00:17:48,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/918f3187bf137060ecc47f7fbe87c8bd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:验证码图片类的编写.doc
本文 PDF 下载地址:验证码图片类的编写.pdf
留言与评论(共有 0 条评论) |