1: ?php 2: /* 3: * file:myvcode.class.php 4: * 验证码类,类名Vcode 5: */ 6: class Vcode 7: { 8: private $width; /*验证码宽度*/ 9: private $height; /*验证码高度*/ 10: private $codeNum; /*验证码字符个数*/ 11: private $checkCode; /*验证码字符*
<span> 1:</span> <?php
<span> 2:</span> <span>/*</span>
<span> 3:</span> <span>* file:myvcode.class.php</span>
<span> 4:</span> <span>* 验证码类,类名Vcode</span>
<span> 5:</span> <span>*/</span>
<span> 6:</span> <span>class</span> Vcode
<span> 7:</span> {
<span> 8:</span> <span>private</span> $width; <span>/*验证码宽度*/</span>
<span> 9:</span> <span>private</span> $height; <span>/*验证码高度*/</span>
<span> 10:</span> <span>private</span> $codeNum; <span>/*验证码字符个数*/</span>
<span> 11:</span> <span>private</span> $checkCode; <span>/*验证码字符*/</span>
<span> 12:</span> <span>private</span> $image; <span>/*验证码资源*/</span>
<span> 13:</span> <span>private</span> $pixNum; <span>/*绘制干扰点的个数*/</span>
<span> 14:</span> <span>private</span> $lineNum; <span>/*绘制干扰线的条数*/</span>
<span> 15:</span>
<span> 16:</span> <span>/*</span>
<span> 17:</span> <span> *构造方法实例化验证码对象,并初始化数据</span>
<span> 18:</span> <span> *@param int $width 设置默认宽度</span>
<span> 19:</span> <span> *@param int $height 设置默认高度</span>
<span> 20:</span> <span> *@param int $codeNum 设置验证码中的字符个数</span>
<span> 21:</span> <span> *@param int $pixNum 设置干扰点的个数</span>
<span> 22:</span> <span> *@param int $lineNum 设置干扰线的数量</span>
<span> 23:</span> <span> */</span>
<span> 24:</span> <span>function</span> __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
<span> 25:</span> {
<span> 26:</span> $this->width = $width;
<span> 27:</span> $this->height = $height;
<span> 28:</span> $this->codeNum = $codeNum;
<span> 29:</span> $this->pixNum = $pixNum;
<span> 30:</span> $this->lineNum = $lineNum;
<span> 31:</span> }
<span> 32:</span> <span>/*内部私有方法,创建图像资源*/</span>
<span> 33:</span> <span>private</span> <span>function</span> getCreateImage()
<span> 34:</span> {
<span> 35:</span> $this->image = imagecreatetruecolor($this->width, $this->height);
<span> 36:</span> $white = imagecolorallocate($this->image,0xff,0xff,0xff);
<span> 37:</span> imagefill($this->image, 0, 0, $white);
<span> 38:</span> $black = imagecolorallocate($this->image,0,0,0);
<span> 39:</span> imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
<span> 40:</span> }
<span> 41:</span> <span>/*内部私有方法,绘制字符,去掉o0Llz和012*/</span>
<span> 42:</span> <span>private</span> <span>function</span> createCheckCode()
<span> 43:</span> {
<span> 44:</span> $code = <span>'3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY'</span>;
<span> 45:</span> $this->checkCode = <span>""</span>;
<span> 46:</span> <span>for</span>($i=0; $icodeNum;$i++)
<span> 47:</span> {
<span> 48:</span> $char = $code{rand(0,strlen($code) - 1)};
<span> 49:</span> $this->checkCode .= $char;
<span> 50:</span> $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
<span> 51:</span> $fontSize = rand(3,5);
<span> 52:</span> $x = rand(0,$this->width-imagefontwidth($fontSize));
<span> 53:</span> $y = rand(0,$this->height-imagefontheight($fontSize));
<span> 54:</span> imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
<span> 55:</span> }
<span> 56:</span> }
<span> 57:</span> <span>/*内部私有方法设置干扰元素*/</span>
<span> 58:</span> <span>private</span> <span>function</span> setDisturbColor()
<span> 59:</span> {
<span> 60:</span> <span>/*绘制干扰点*/</span>
<span> 61:</span> <span>for</span>($i=0; $ipixNum; $i++)
<span> 62:</span> {
<span> 63:</span> $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
<span> 64:</span> imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
<span> 65:</span> }
<span> 66:</span> <span>/*绘制干扰线*/</span>
<span> 67:</span> <span>for</span>($i=0; $ilineNum; $i++)
<span> 68:</span> {
<span> 69:</span> $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
70: imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),
rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);
<span> 71:</span> }
<span> 72:</span> }
<span> 73:</span> <span>/*开启session保存 利用echo 输出图像*/</span>
<span> 74:</span> <span>function</span> __toString()
<span> 75:</span> {
<span> 76:</span> $_SESSION[<span>'code'</span>] = strtoupper($this->checkCode);
<span> 77:</span> $this->getCreateImage();
<span> 78:</span> $this->createCheckCode();
<span> 79:</span> $this->setDisturbColor();
<span> 80:</span> $this->outputImg();
<span> 81:</span> }
<span> 82:</span> <span>/*内部私有方法输出图像*/</span>
<span> 83:</span> <span>private</span> <span>function</span> outputImg()
<span> 84:</span> {
<span> 85:</span> header(<span>"content-type:image/png"</span>);
<span> 86:</span> imagepng($this->image);
<span> 87:</span> }
<span> 88:</span> <span>/*析构方法,释放对象*/</span>
<span> 89:</span> <span>function</span> __destruct()
<span> 90:</span> {
<span> 91:</span> imagedestroy($this->image);
<span> 92:</span> }
<span> 93:</span> }
<span> 94:</span> ?>