標籤:des style blog http color io os ar java
PHP製作驗證碼詳細教程
效果:
myvcode.class.php:封裝建立驗證碼的類
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; /*驗證碼字元*/
12: private $image; /*驗證碼資源*/
13: private $pixNum; /*繪製幹擾點的個數*/
14: private $lineNum; /*繪製幹擾線的條數*/
15:
16: /*
17: *構造方法執行個體化驗證碼對象,並初始化資料
18: *@param int $width 設定預設寬度
19: *@param int $height 設定預設高度
20: *@param int $codeNum 設定驗證碼中的字元個數
21: *@param int $pixNum 設定幹擾點的個數
22: *@param int $lineNum 設定幹擾線的數量
23: */
24: function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
25: {
26: $this->width = $width;
27: $this->height = $height;
28: $this->codeNum = $codeNum;
29: $this->pixNum = $pixNum;
30: $this->lineNum = $lineNum;
31: }
32: /*內部私人方法,建立映像資源*/
33: private function getCreateImage()
34: {
35: $this->image = imagecreatetruecolor($this->width, $this->height);
36: $white = imagecolorallocate($this->image,0xff,0xff,0xff);
37: imagefill($this->image, 0, 0, $white);
38: $black = imagecolorallocate($this->image,0,0,0);
39: imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
40: }
41: /*內部私人方法,繪製字元,去掉o0Llz和012*/
42: private function createCheckCode()
43: {
44: $code = ‘3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY‘;
45: $this->checkCode = "";
46: for($i=0; $i<$this->codeNum;$i++)
47: {
48: $char = $code{rand(0,strlen($code) - 1)};
49: $this->checkCode .= $char;
50: $fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
51: $fontSize = rand(3,5);
52: $x = rand(0,$this->width-imagefontwidth($fontSize));
53: $y = rand(0,$this->height-imagefontheight($fontSize));
54: imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
55: }
56: }
57: /*內部私人方法設定幹擾元素*/
58: private function setDisturbColor()
59: {
60: /*繪製幹擾點*/
61: for($i=0; $i<$this->pixNum; $i++)
62: {
63: $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
64: imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
65: }
66: /*繪製幹擾線*/
67: for($i=0; $i<$this->lineNum; $i++)
68: {
69: $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);
71: }
72: }
73: /*開啟session儲存 利用echo 輸出映像*/
74: function __toString()
75: {
76: $_SESSION[‘code‘] = strtoupper($this->checkCode);
77: $this->getCreateImage();
78: $this->createCheckCode();
79: $this->setDisturbColor();
80: $this->outputImg();
81: }
82: /*內部私人方法輸出映像*/
83: private function outputImg()
84: {
85: header("content-type:image/png");
86: imagepng($this->image);
87: }
88: /*析構方法,釋放對象*/
89: function __destruct()
90: {
91: imagedestroy($this->image);
92: }
93: }
94: ?>
imgcode.php輸出映像
1: <?php
2: session_start();
3: require_once(‘myvcode.class.php‘);
4: echo new Vcode();
5: ?>
test.html:同過img標籤引用
1: <img src="imgcode.php">
可以加一個a標籤,用js實現換一張效果:
/*局部重新整理換驗證碼*/function changeCode(){var imgcode = document.getElementById(‘code‘);var change = document.getElementById(‘change‘);change.onclick = function(){/*必須加後面的參數才能重新整理*/imgcode.src=‘code.php?tm‘+Math.random();}}
code和change分別是img和a的id
PHP製作驗證碼