支持可定制 是否点干扰,线干扰,字符是否倾斜及有无边框等
用法:
<?php /** * 验证码demo * * @author zhaoxuejie<[email protected]> * @date 2012-2-20 * @version v1.0 */ session_start(); require_once('validate.class.php'); $image = new Validate(100, 20, 5);//图片宽度、高度、字符个数 $image->isOblique = false;//字符是否倾斜 //$image->isPoint = false;//字符是否显示点干扰 //$image->isLine = false;//字符是否显示线干扰 //$image->isBorder = false;//是否需要边框 $image->outputImg(); $_SESSION['authCode'] = $image->authCode; //存贮验证码到 $_SESSION 中 ?>
<?php /** * 验证码共用类 * * @author zhaoxuejie<[email protected]> * @date 2012-2-20 * @version v1.0 * * demo * ------------------ * require_once('validate.class.php'); * $image = new validate(80, 25, 4);//图片宽度、高度、字符个数--可省略 * $image->outputImg(); * --------------------------------------- */ class Validate { private $width; //验证码宽度 private $height; //验证码高度 private $codeNum; //验证码字符个数 private $codeContent = "2345689QWERTYUPASDFGHJKXCVBNM";//验证码字符内容 private $checkImage; //验证码图片 private $disturbColor = '';//干扰像素 public $noisePoint = 100;//点干扰数量 public $noiseLine = 8;//点干扰数量 public $font = './fonts/simhei.ttf';//字体,只有选中倾斜的时候 即$isOblique = true的时候有用 public $isPoint = true;//是否显示点干扰 public $isLine = true;//是否显示线干扰 public $isOblique = true;//字符是否倾斜 public $isBorder = true;//是否需要边框 public $authCode; //产生的验证码 //初始化 function __construct($width=80, $height=21, $codeNum=4) { $this->width=$width; $this->height=$height; $this->codeNum=$codeNum; $this->createCode();//产生验证码 } //兼容php4 function Validate($width=80, $height=21, $codeNum=4){ $this->__construct($width, $height, $codeNum); } function outputImg() { $this->outFileHeader();//输出头 $this->createImage();//产生图片 $this->setDisturbColor();//设置干扰像素 $this->writeCheckCodeToImage();//往图片上写验证码 $this->createValidate();//生成验证码图片 } //生成验证码 private function createCode(){ for ($i = 0; $i < $this->codeNum; $i++) { $this->authCode .= $this->codeContent{rand(0, strlen($this->codeContent) - 1)}; } } //头图片类型 private function outFileHeader() { Header("Content-type:image/png"); } //产生图片 private function createImage() { $this->checkimage = @imagecreate($this->width,$this->height); $back = imagecolorallocate($this->checkimage,255,255,255); if($this->isBorder){ $border = imagecolorallocate($this->checkimage,0,0,0); }else{ $border = imagecolorallocate($this->checkimage,255,255,255); } imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底 imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框 } //设置干扰因素 private function setDisturbColor() { if($this->isPoint){ $this->createNoisePoint();//设置点干扰 } if($this->isLine){ $this->createNoiseLine();//设置线干扰 } } //设置点干扰 public function createNoisePoint(){ for ($i = 0; $i <= $this->noisePoint; $i++) { $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255)); imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor); } } //设置弧线干扰 public function createNoiseLine(){ for ($i = 0; $i <= $this->noiseLine; $i++) { $color = imagecolorallocate($this->checkimage, rand(128, 255), rand(125, 255), rand(100, 255)); imagearc($this->checkimage, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color); } } //验证码写入图片 private function writeCheckCodeToImage() { for ($i=0;$i<=$this->codeNum;$i++) { $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255)); $size = rand(floor($this->height / 5), floor($this->height / 3)); $x = (5+floor($this->width/$this->codeNum)*$i); if($this->isOblique){ $y = rand(floor($this->height/2)+5, floor($this->height/2)+5); imagettftext($this->checkimage, rand(11, 14), rand(-35,35), $x, $y, $bg_color, $this->font, $this->authCode[$i]); }else{ $y = ($this->height/2 - rand(3,10)); imagechar ($this->checkimage, rand(5,12), $x, $y, $this->authCode[$i], $bg_color); } } } //生成验证码图片 private function createValidate(){ imagepng($this->checkimage); imagedestroy($this->checkimage); } //获得验证码 public function getCode(){ return $this->authCode; } function __destruct() { unset($this->width,$this->height,$this->codeNum); } } ?>