PHP验证码

 <?php

 
/***********************************************
 *   *
 * @验证码类   *
 * @版本   1.0   *
 * @作者   军   *
 * @时间   2010-07-02   *
 *   *
 **********************************************/
 
class CheckCodeFile{
 
private $_codenum    = 4;        //验证码的位数
 
private $_codestr    = '';       //产生的验证码
 
private $_codeimg    = '';       //验证码图片
 
private $_codepix    = '';       //干扰素
 
private $_codewidth;             //验证码图片的宽度
 
private $_codeheight;            //验证码图片的高度
 
/**
* 构造函数
*/
public function __construct($w = 80,$h = 20){
session_start();
$this->_codewidth  = $w;
$this->_codeheight = $h;
}
/**
* 输出头部信息
* 输出为GIF的图片
*/
private function OutFileHeader(){
header("Content-type: image/png"); 
}//End OutFileHeader
/**
* 产生验证码字符
*/
private  function CreateCheckCodeStr(){
$this->_codestr = strtoupper(substr(md5(rand()),0,$this->_codenum));
//$_SESSION[code] = $this->_codestr;
return $this->_codestr;
}//End CreateCheckCode
/**
* 产生验证码图片
*/
private function CreateCheckCodeImg(){
$this->_codeimg = imagecreate($this->_codewidth,$this->_codeheight);
imagecolorallocate($this->_codeimg,200,200,200);
return $this->_codeimg;
}//End CreateCheckCodeImg
/**
* 设置验证图片的干扰素
*/
private function SetPixColor(){
for ($i=0;$i<128;$i++){
$this->_codepix = imagecolorallocate($this->_codeimg,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->_codeimg,rand(2,128),rand(2,38),$this->_codepix);
}
}//End SetPixColor
/**
* 在验证图片上画上验证码的字符
*/
private function WriteCheckCodeToImg(){
for ($i=0;$i<$this->_codenum;$i++){
$bgColor = imagecolorallocate($this->_codeimg,rand(0,255),rand(0,255),rand(0,255));
$x       = floor($this->_codewidth/$this->_codenum)*$i;
$y       = rand(0,$this->_codeheight-15);
imagechar($this->_codeimg,5,$x+10,$y,$this->_codestr[$i],$bgColor);
}
}//End WriteCheckCodeImg
/**
* 输出验证码图片
*/
public function OutChceckImage(){
$this->OutFileHeader();
$this->CreateCheckCodeStr();
$this->CreateCheckCodeImg();
$this->SetPixColor();
$this->WriteCheckCodeToImg();
imagepng($this->_codeimg);
imagedestroy($this->_codeimg);
}//End OutChceckImage
}//End Class
 
 
 
//例子
//$code = new CheckCodeFile(80,30);
//$code->OutChceckImage();
 
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值