一个php的验证码类
一个PHP生成的英文验证码类
一个PHP生成的英文验证码类
<?php
class verificationCode{
private $image;
private $width;
private $height;
private $pixersum; //干扰点的数目
private $codenum;//验证字符数目
private $codes;//字符
function __construct($width="100", $height="50", $codenum="4"){
$this->width=$width;
$this->height=$height;
$this->codenum=$codenum;
$this->codes=$this->strings();
$this->image=imagecreatetruecolor($this->width, $this->height);
$this->pixersum=floor($width*$height/50);
}
function showCode(){
//画布颜色
$this->bulidcolor();
//画干扰素
$this->bulidgrs();
//画字符
$this->bulidzifu();
//输出并销毁
$this->show();
}
function tocode(){
return $this->codes;
}
private function bulidcolor(){
//背景颜色
$red=rand(220, 255);
$green=rand(220, 255);
$bule=rand(220, 255);
$color=imagecolorallocate($this->image, $red, $green, $bule);
imagefill($this->image, 0, 0, $color);
//黑色边框
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->image, 0, 0, 0));
}
private function bulidgrs(){
//画点
for($i=0; $i<$this->pixersum; $i++){
$color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
}
//画弧线
for($i=0; $i<15; $i++){
$color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(10, $this->width), rand(10, $this->height), rand(-30, 30), rand(-30, 30), $color);
}
}
private function strings(){
$str="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$strs="";
for($i=0; $i<$this->codenum; $i++){
$cha=$str{rand(0, strlen($str)-1)};
$strs.=$cha;
}
return $strs;
}
private function bulidzifu(){
for($i=0; $i<$this->codenum; $i++){
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
$fontsize=rand(4, 5);
$x=floor($this->width/$this->codenum)*$i+3;
$y=rand(0, $this->height-15);
imagechar($this->image, $fontsize, $x, $y, $this->codes{$i},$fontcolor);
}
}
private function show(){
header("Content-Type:image/gif");
imagegif($this->image);
imagedestroy($this->image);
}
}
?>
精彩图集
精彩文章






