PHP學習筆記09——GD生成驗證碼實例

vcode.class.php

 

 1 <?php
 2     class Vcode{
 3         private $width;
 4         private $height;
 5         private $codeNum;
 6         private $disturbColorNum;    //干擾元素數量
 7         private $checkCode;
 8         private $image;
 9 
10         //構造方法,參數初始化,並生成驗證碼
11         function __construct($width = 80, $height = 20, $codeNum = 4) {
12             $this->width = $width;
13             $this->height = $height;
14             $this->codeNum = $codeNum;
15             $number = floor($height * $width / 15);
16             //計算干擾元素數量
17             if ($number > 240 - $codeNum) 
18                 $this->disturbColorNum = 240 - $codeNum;
19             else 
20                 $this->disturbColorNum = $number;
21             //建立驗證碼
22             $this->checkCode = $this->createCheckCode();
23         }
24         //析構方法,銷燬圖片資源
25         function __destruct(){
26             imagedestroy($this->image);
27         }
28         //隨機生成驗證碼
29         private function createCheckCode(){
30             $code = "3456789abcdeffghijkmnpqrstuvwxyzABCDEFHIJKMNPQRSTUVWXYZ";
31             $ascii="";
32             for ($i = 0; $i < $this->codeNum; $i++) {
33                 $ascii .= $code{rand(0,strlen($code)-1)};
34             }
35             return $ascii;
36         }
37         //用於輸出驗證碼圖片
38         function __toString(){
39             $_SESSION["code"] = strtoupper($this->checkCode);
40             $this->outimg();
41             return "";
42         }
43         //輸出圖片的具體步驟
44         private function outimg(){
45             $this->getCreateImage();
46             $this->setDisturbcolor();
47             $this->outputText();
48             $this->outputImage();
49         }
50         //初始化圖片
51         private function getCreateImage(){
52             $this->image = imagecreatetruecolor($this->width, $this->height);
53             $backColor = imagecolorallocate($this->image, rand(225,255), rand(225,255), rand(225,255));
54             imagefill($this->image, 0, 0, $backColor);
55             $borderColor = imagecolorallocate($this->image, 0, 0, 0);
56             imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $borderColor);
57         }
58         //輸出不一樣顏色的點做爲干擾素,點加弧線
59         private function setDisturbcolor(){
60             for ($i = 0; $i <= $this->disturbColorNum; $i++) {
61                 $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
62                 imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
63             }
64             for ($i = 0; $i <10; $i++) {
65                 $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
66                 imagearc($this->image, rand(-10,$this->width), rand(-10,$this->height), rand(30,300), 
67                     rand(20,200), 55, 44, $color);
68             }    
69         }
70         //隨機在圖片中生成字符串
71         private function outputText(){
72             for ($i = 0; $i < $this->codeNum; $i++) {
73                 $fontcolor = imagecolorallocate($this->image, rand(0,128), rand(0,128), rand(0,128));
74                 $fontsize = rand(4, 5);
75                 $x = floor($this->width/$this->codeNum)*$i + 3;
76                 $y = rand(0, $this->height-imagefontheight($fontsize));
77                 imagechar($this->image, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor);
78             }
79         }
80         //輸出圖片
81         private function outputImage(){
82             header("Content-type: image/png");
83             imagepng($this->image);
84         }
85         
86     }
87 ?>

 

 

imgcode.php

 

1 <?php
2     session_start();                    //開啓session
3     require_once ('vcode.class.php');        //包含驗證碼文件
4     echo new Vcode();
5 ?>

 

 

image.php

 

 1 <?php
 2     session_start();    //開啓session
 3     
 4     if (isset($_POST['submit'])) {
 5         if (strtoupper($_POST['code']) == $_SESSION['code']) 
 6             echo "驗證碼輸入正確<br/>";
 7         else 
 8             echo '<font color="red">驗證碼輸入錯誤<font/><br/>';
 9     }
10 ?>
11 <html>
12     <head>
13         <script type="text/javascript">
14             function newgdcode(obj, url) {
15                 //加一個隨機參數以避免瀏覽器不刷新
16                 obj.src = url+'?nowtime='+new Date().getTime();
17             }
18         </script>
19     </head>
20     <body>
21         <img alt="看不清楚,換一張" src="imgcode.php" style="cursor: pointer;" 
22                         onclick="javascript:newgdcode(this, this.src);" />
23         <form action="image.php" method="post">
24             <input type="text" size="4" name="code" />
25             <input type="submit" name="submit" value="提交" />
26         </form>
27     </body>
28 </html>

 

 

執行結果javascript

相關文章
相關標籤/搜索