Home > php教程 > php手册 > body text

PHP制作验证码

WBOY
Release: 2016-06-06 19:47:03
Original
821 people have browsed it

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 
Copy after login
<span>   2:</span> <span>/*</span>
Copy after login
<span>   3:</span> <span>* file:myvcode.class.php</span>
Copy after login
<span>   4:</span> <span>* 验证码类,类名Vcode</span>
Copy after login
<span>   5:</span> <span>*/</span>
Copy after login
<span>   6:</span> <span>class</span> Vcode
Copy after login
<span>   7:</span> {
Copy after login
<span>   8:</span>     <span>private</span> $width;              <span>/*验证码宽度*/</span>
Copy after login
<span>   9:</span>     <span>private</span> $height;             <span>/*验证码高度*/</span>
Copy after login
<span>  10:</span>     <span>private</span> $codeNum;            <span>/*验证码字符个数*/</span>
Copy after login
<span>  11:</span>     <span>private</span> $checkCode;            <span>/*验证码字符*/</span>
Copy after login
<span>  12:</span>     <span>private</span> $image;                <span>/*验证码资源*/</span>
Copy after login
<span>  13:</span>     <span>private</span> $pixNum;            <span>/*绘制干扰点的个数*/</span>
Copy after login
<span>  14:</span>     <span>private</span> $lineNum;            <span>/*绘制干扰线的条数*/</span>
Copy after login
<span>  15:</span>
Copy after login
<span>  16:</span>     <span>/*</span>
Copy after login
<span>  17:</span> <span>    *构造方法实例化验证码对象,并初始化数据</span>
Copy after login
<span>  18:</span> <span>    *@param int $width         设置默认宽度</span>
Copy after login
<span>  19:</span> <span>    *@param int $height     设置默认高度</span>
Copy after login
<span>  20:</span> <span>    *@param int $codeNum    设置验证码中的字符个数</span>
Copy after login
<span>  21:</span> <span>    *@param int $pixNum        设置干扰点的个数</span>
Copy after login
<span>  22:</span> <span>    *@param int $lineNum    设置干扰线的数量</span>
Copy after login
<span>  23:</span> <span>    */</span>
Copy after login
<span>  24:</span>     <span>function</span> __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
Copy after login
<span>  25:</span>     {
Copy after login
<span>  26:</span>         $this->width = $width;
Copy after login
<span>  27:</span>         $this->height = $height;
Copy after login
<span>  28:</span>         $this->codeNum = $codeNum;
Copy after login
<span>  29:</span>         $this->pixNum = $pixNum;
Copy after login
<span>  30:</span>         $this->lineNum = $lineNum;
Copy after login
<span>  31:</span>     }
Copy after login
<span>  32:</span>     <span>/*内部私有方法,创建图像资源*/</span>
Copy after login
<span>  33:</span>     <span>private</span> <span>function</span> getCreateImage()
Copy after login
<span>  34:</span>     {
Copy after login
<span>  35:</span>         $this->image = imagecreatetruecolor($this->width, $this->height);
Copy after login
<span>  36:</span>         $white = imagecolorallocate($this->image,0xff,0xff,0xff);
Copy after login
<span>  37:</span>         imagefill($this->image, 0, 0, $white);
Copy after login
<span>  38:</span>         $black = imagecolorallocate($this->image,0,0,0);
Copy after login
<span>  39:</span>         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
Copy after login
<span>  40:</span>     }
Copy after login
<span>  41:</span>     <span>/*内部私有方法,绘制字符,去掉o0Llz和012*/</span>
Copy after login
<span>  42:</span>     <span>private</span> <span>function</span> createCheckCode()
Copy after login
<span>  43:</span>     {
Copy after login
<span>  44:</span>         $code = <span>'3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY'</span>;
Copy after login
<span>  45:</span>         $this->checkCode = <span>""</span>;
Copy after login
<span>  46:</span>         <span>for</span>($i=0; $icodeNum;$i++)
Copy after login
<span>  47:</span>         {
Copy after login
<span>  48:</span>             $char = $code{rand(0,strlen($code) - 1)};
Copy after login
<span>  49:</span>             $this->checkCode .= $char;
Copy after login
<span>  50:</span>             $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
Copy after login
<span>  51:</span>             $fontSize = rand(3,5);
Copy after login
<span>  52:</span>             $x = rand(0,$this->width-imagefontwidth($fontSize));
Copy after login
<span>  53:</span>             $y = rand(0,$this->height-imagefontheight($fontSize));
Copy after login
<span>  54:</span>             imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
Copy after login
<span>  55:</span>         }
Copy after login
<span>  56:</span>     }
Copy after login
<span>  57:</span>     <span>/*内部私有方法设置干扰元素*/</span>
Copy after login
<span>  58:</span>     <span>private</span> <span>function</span> setDisturbColor()
Copy after login
<span>  59:</span>     {
Copy after login
<span>  60:</span>         <span>/*绘制干扰点*/</span>
Copy after login
<span>  61:</span>         <span>for</span>($i=0; $ipixNum; $i++)
Copy after login
<span>  62:</span>         {
Copy after login
<span>  63:</span>             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
Copy after login
<span>  64:</span>             imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
Copy after login
<span>  65:</span>         }
Copy after login
<span>  66:</span>         <span>/*绘制干扰线*/</span>
Copy after login
<span>  67:</span>         <span>for</span>($i=0; $ilineNum; $i++)
Copy after login
<span>  68:</span>         {
Copy after login
<span>  69:</span>             $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
Copy after login

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>         }
Copy after login
<span>  72:</span>     }
Copy after login
<span>  73:</span>     <span>/*开启session保存 利用echo 输出图像*/</span>
Copy after login
<span>  74:</span>     <span>function</span> __toString()
Copy after login
<span>  75:</span>     {
Copy after login
<span>  76:</span>         $_SESSION[<span>'code'</span>] = strtoupper($this->checkCode);
Copy after login
<span>  77:</span>         $this->getCreateImage();
Copy after login
<span>  78:</span>         $this->createCheckCode();
Copy after login
<span>  79:</span>         $this->setDisturbColor();
Copy after login
<span>  80:</span>         $this->outputImg();
Copy after login
<span>  81:</span>     }
Copy after login
<span>  82:</span>     <span>/*内部私有方法输出图像*/</span>
Copy after login
<span>  83:</span>     <span>private</span> <span>function</span> outputImg()
Copy after login
<span>  84:</span>     {
Copy after login
<span>  85:</span>         header(<span>"content-type:image/png"</span>);
Copy after login
<span>  86:</span>         imagepng($this->image);
Copy after login
<span>  87:</span>     }
Copy after login
<span>  88:</span>     <span>/*析构方法,释放对象*/</span>
Copy after login
<span>  89:</span>     <span>function</span> __destruct()
Copy after login
<span>  90:</span>     {
Copy after login
<span>  91:</span>         imagedestroy($this->image);
Copy after login
<span>  92:</span>     }
Copy after login
<span>  93:</span> }
Copy after login
<span>  94:</span> ?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template