html' target='_blank'>PHP制作驗證碼詳細教程
效果:
myvcode.class.php:封裝創(chuàng)建驗證碼的類
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: /*內部私有方法,創(chuàng)建圖像資源*/
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(); }}
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
|
新聞熱點
疑難解答