教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP验证码例子(带刷新)DEMO_PHP图片验证码类实例

PHP验证码例子(带刷新)DEMO_PHP图片验证码类实例

发布时间:2016-11-26   编辑:jiaochengji.com
教程集为您提供PHP验证码例子(带刷新)DEMO,PHP图片验证码类实例等资源,欢迎您收藏本站,我们将为您提供最新的PHP验证码例子(带刷新)DEMO,PHP图片验证码类实例资源
验证码这样的功能可以说是无处不在了、那使用php来实现验证码这样的功能呢?接下来我就将验证码实现封装到一个类里面独立出来、那么后面如果再使用到验证码功能。

直接引入该类文件并创建该类的实例、就可以使用验证码了,验证码类文件vcode.class.php代码如下

<pre name="code" style="list-style: none; margin: 8px; padding: 3px; color: rgb(68, 68, 68); outline: none; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: rgb(187, 187, 187); direction: ltr; font-stretch: normal; font-size: 14px; line-height: 22px; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; overflow-x: visible; overflow-y: hidden; width: 600px; white-space: pre-wrap; word-wrap: break-word; background-image: url(http://zhuqidong.gotoip2.com/templets/caizi/images/code-bg.png); background-color: rgb(255, 255, 255);">//验证码类 class Vcode{ private $width;//图片宽度 private $height;//图片高度 private $num;//验证码个数 private $img;//图片资源 private $code;//验证码 private $pointNum;//干扰点个数 private $lineNum;//干扰线个数 private $fontFile;//字体文件   //构造函数初始化相关数据 function __construct($width=85,$height=34,$num=4){ $this->width=$width; $this->height=$height; $this->num=$num; $this->code=$this->createCode(); $this->pointNum=100; $this->lineNum=10; $this->fontFile="<a href="http://dwtedx.com/font/无"  target="_blank" class="keylink">STL</a>ITI.TTF"; }   //用于设置成员属性 //@param string $key 成员属性名 //@param mixed $value 成员属性值 //@return object 返回自己对象$this,可用于连贯操作 public function set($key,$val){ //get_class_vars() 获取类中的属性组成的数组 //get_class() 返回对象的类名 if(array_key_exists($key,get_class_vars(get_class($this)))){ $this->setOption($key,$val); } return $this; } //设置参数 private function setOption($key,$value){ $this->$key=$value; } //获取验证码 public function getCode(){ return $this->code; }   //输出图像 public function outImg(){ //创建图像 $this->createImage(); //画验证码 $this->drawCode(); //画干扰元素 $this->drawDisturbColor(); //输出图像 $this->printImg(); }   //画验证码 private function drawCode(){ $this->fontFile="./font/".$this->fontFile; for($i=0;$i<$this->num;$i ){ //设置随机颜色 $randColor=imagecolorallocate($this->img,rand(0,128) ,rand(0,128),rand(0,128)); //字体大小 $fontSize=rand(20,23); //字体水平位置 $x=($this->width/$this->num)*$i; //水平方向的位置 $y=rand($fontSize,imagefontheight($fontSize) 3); //画字体 imagettftext($this->img,$fontSize,0,$x,$y,$randColor, $this->fontFile,$this->code{$i}); } } //画干扰元素 private function drawDisturbColor(){ //画干扰点 for($i=0;$i<$this->pointNum;$i ){ //设置随机颜色 $randColor=imagecolorallocate($this->img,rand(0,255), rand(0,255),rand(0,255)); //画点 imagesetpixel($this->img,rand(1,$this->width-2),rand(1, $this->height-2),$randColor); } //画干扰线 for($i=0;$i<$this->lineNum;$i ){ //设置随机颜色 $randColor=imagecolorallocate($this->img,rand(0,200), rand(0,200),rand(0,200)); //画线 imageline($this->img,rand(1,$this->width-2),rand(1, $this->height-2),rand(1,$this->height-2), rand(1,$this->width-2),$randColor); } }   //创建图像 private function createImage(){ //创建一个真彩色图像 $this->img=imagecreatetruecolor($this->width,$this->height); //设置背景色 $bgColor=imagecolorallocate($this->img,rand(200,255), rand(200,255),rand(200,255)); //填充背景色 imagefill($this->img,0,0,$bgColor); //设置边框颜色 $borderColor=imagecolorallocate($this->img,0,0,0); //画一个边框 imagerectangle($this->img,0,0,$this->width-1, $this->height-1,$borderColor); }   //输出图像 private function printImg(){ if(imagetypes() & IMG_PNG){ //针对png header("Content-Type:image/png"); imagepng($this->img); }else if(imagetypes() & IMG_JPG){ //针对jpg header("Content-Type:image/jpeg"); imagejpeg($this->img,null,100); }else if(imagetypes() & IMG_GIF){ //针对Gif header("Content-Type:image/gif"); imagegif($this->img); }else if(imagetypes() & IMG_WBMP){ // 针对 WBMP header(′Content-Type: image/vnd.wap.wbmp′); imagewbmp($this->img); }else{ die(′No image support in this PHP server′); } }   //创建验证码 private function createCode(){ //默认字符串 $codes="123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"; //生成验证码 $code=""; for($i=0;$i<$this->num;$i ){ $code.=$codes{rand(0,strlen($codes)-1)}; } return $code; }   //析构函数用于销毁图像资源 function __destruct(){ imagedestroy($this->img); } }</pre>

这里我使用的画字体的函数是imagettftext()、因为这个函数可以自定义字体样式、从代码中也能看出来

传入的参数有个字体文件属性、如果不喜欢用这个函数可以使用imagestring()函数也行

只不过个人觉得这个函数的默认字体大小、也不好看、还是自定义字体看着舒服点

调用验证码类image_002.php代码如下

<pre name="code" style="list-style: none; margin: 8px; padding: 3px; color: rgb(68, 68, 68); outline: none; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: rgb(187, 187, 187); direction: ltr; font-stretch: normal; font-size: 14px; line-height: 22px; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; overflow-x: visible; overflow-y: hidden; width: 600px; white-space: pre-wrap; word-wrap: break-word; background-image: url(http://zhuqidong.gotoip2.com/templets/caizi/images/code-bg.png); background-color: rgb(255, 255, 255);">//开启Sessionsession_start();//引入验证码类include("vcode.class.php");//创建验证码类$vcode=new Vcode();//将获取的验证码存入到session中$_SESSION[′code′]=$vcode->getCode();//$vcode->set("pointNum",10);//自定义干扰点个数//$vcode->set("lineNum",10);//自定义干扰线个数//$vcode->set("fontFile","wawa.ttf");//自定义字体文件//输出图像$vcode->outImg();</pre>

HTML代码如下

<pre name="code" style="list-style: none; margin: 8px; padding: 3px; color: rgb(68, 68, 68); outline: none; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: rgb(187, 187, 187); direction: ltr; font-stretch: normal; font-size: 14px; line-height: 22px; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; overflow-x: visible; overflow-y: hidden; width: 600px; white-space: pre-wrap; word-wrap: break-word; background-image: url(http://zhuqidong.gotoip2.com/templets/caizi/images/code-bg.png); background-color: rgb(255, 255, 255);"><img src="image_002.php" onclick="this.src=′image_002.php?Math.random()′"/></pre>

到这里整个功能就做完了、希望对一些哥们有用、同时也当自己做个笔记

您可能感兴趣的文章:
php验证码大全(实例分享)
php彩色验证码的简单例子
php5验证码类(简易实用型)
php点击验证码实时刷新的实现代码
php验证码的三个实例代码分享
php图片验证码的例子
php生成扭曲及旋转的验证码图片的实例代码
php验证码(GD库生成验证码)的例子
用php生成带有雪花背景的验证码
php验证码简单函数代码(附效果图)

[关闭]
~ ~