国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP驗(yàn)證碼例子(帶刷新)DEMO_PHP圖片驗(yàn)證碼類實(shí)例

2024-09-04 11:44:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

驗(yàn)證碼這樣的功能可以說(shuō)是無(wú)處不在了、那使用php來(lái)實(shí)現(xiàn)驗(yàn)證碼這樣的功能呢?接下來(lái)我就將驗(yàn)證碼實(shí)現(xiàn)封裝到一個(gè)類里面獨(dú)立出來(lái)、那么后面如果再使用到驗(yàn)證碼功能。

直接引入該類文件并創(chuàng)建該類的實(shí)例、就可以使用驗(yàn)證碼了,驗(yàn)證碼類文件vcode.class.php代碼如下:

  1. //驗(yàn)證碼類 
  2. class Vcode{ 
  3. private $width;//圖片寬度 
  4. private $height;//圖片高度 
  5. private $num;//驗(yàn)證碼個(gè)數(shù) 
  6. private $img;//圖片資源 
  7. private $code;//驗(yàn)證碼 
  8. private $pointNum;//干擾點(diǎn)個(gè)數(shù) 
  9. private $lineNum;//干擾線個(gè)數(shù) 
  10. private $fontFile;//字體文件 
  11.  
  12. //構(gòu)造函數(shù)初始化相關(guān)數(shù)據(jù) 
  13. function __construct($width=85,$height=34,$num=4){ 
  14. $this->width=$width
  15. $this->height=$height
  16. $this->num=$num
  17. $this->code=$this->createCode(); 
  18. $this->pointNum=100; 
  19. $this->lineNum=10; 
  20. $this->fontFile="<a href="http://dwtedx.com/font/無(wú)"  
  21. target="_blank" class="keylink">STL</a>ITI.TTF"; 
  22.  
  23. //用于設(shè)置成員屬性 
  24. //@param string $key 成員屬性名 
  25. //@param mixed $value 成員屬性值 
  26. //@return object 返回自己對(duì)象$this,可用于連貫操作 
  27. public function set($key,$val){ 
  28. //get_class_vars() 獲取類中的屬性組成的數(shù)組 
  29. //get_class() 返回對(duì)象的類名 
  30. if(array_key_exists($key,get_class_vars(get_class($this)))){ 
  31. $this->setOption($key,$val); 
  32. return $this
  33. //設(shè)置參數(shù) 
  34. private function setOption($key,$value){ 
  35. $this->$key=$value
  36.  
  37. //獲取驗(yàn)證碼 
  38. public function getCode(){ 
  39. return $this->code; 
  40.  
  41. //輸出圖像 
  42. public function outImg(){ 
  43. //創(chuàng)建圖像 
  44. $this->createImage(); 
  45. //畫(huà)驗(yàn)證碼 
  46. $this->drawCode(); 
  47. //畫(huà)干擾元素 
  48. $this->drawDisturbColor(); 
  49. //輸出圖像 
  50. $this->printImg(); 
  51.  
  52. //畫(huà)驗(yàn)證碼 
  53. private function drawCode(){ 
  54. $this->fontFile="./font/".$this->fontFile; 
  55. for($i=0;$i<$this->num;$i++){ 
  56. //設(shè)置隨機(jī)顏色 
  57. $randColor=imagecolorallocate($this->img,rand(0,128) 
  58. ,rand(0,128),rand(0,128)); 
  59. //字體大小 
  60. $fontSize=rand(20,23); 
  61. //字體水平位置 
  62. $x=($this->width/$this->num)*$i
  63. //水平方向的位置 
  64. $y=rand($fontSize,imagefontheight($fontSize)+3); 
  65. //畫(huà)字體 
  66. imagettftext($this->img,$fontSize,0,$x,$y,$randColor
  67. $this->fontFile,$this->code{$i}); 
  68.  
  69. //畫(huà)干擾元素 
  70. private function drawDisturbColor(){ 
  71. //畫(huà)干擾點(diǎn) 
  72. for($i=0;$i<$this->pointNum;$i++){ 
  73. //設(shè)置隨機(jī)顏色 
  74. $randColor=imagecolorallocate($this->img,rand(0,255), 
  75. rand(0,255),rand(0,255)); 
  76. //畫(huà)點(diǎn) 
  77. imagesetpixel($this->img,rand(1,$this->width-2),rand(1, 
  78. $this->height-2),$randColor); 
  79. //畫(huà)干擾線 
  80. for($i=0;$i<$this->lineNum;$i++){ 
  81. //設(shè)置隨機(jī)顏色 
  82. $randColor=imagecolorallocate($this->img,rand(0,200), 
  83. rand(0,200),rand(0,200)); 
  84. //畫(huà)線 
  85. imageline($this->img,rand(1,$this->width-2),rand(1, 
  86. $this->height-2),rand(1,$this->height-2), 
  87. rand(1,$this->width-2),$randColor); 
  88.  
  89. //創(chuàng)建圖像 
  90. private function createImage(){ 
  91. //創(chuàng)建一個(gè)真彩色圖像 
  92. $this->img=imagecreatetruecolor($this->width,$this->height); 
  93. //設(shè)置背景色 
  94. $bgColor=imagecolorallocate($this->img,rand(200,255), 
  95. rand(200,255),rand(200,255)); 
  96. //填充背景色 
  97. imagefill($this->img,0,0,$bgColor); 
  98. //設(shè)置邊框顏色 
  99. $borderColor=imagecolorallocate($this->img,0,0,0); 
  100. //畫(huà)一個(gè)邊框 
  101. imagerectangle($this->img,0,0,$this->width-1, 
  102. $this->height-1,$borderColor); 
  103.  
  104. //輸出圖像 
  105. private function printImg(){ 
  106. if(imagetypes() & IMG_PNG){ 
  107. //針對(duì)png 
  108. header("Content-Type:image/png"); 
  109. imagepng($this->img); 
  110. }else if(imagetypes() & IMG_JPG){ 
  111. //針對(duì)jpg 
  112. header("Content-Type:image/jpeg"); 
  113. imagejpeg($this->img,null,100); 
  114. }else if(imagetypes() & IMG_GIF){ 
  115. //針對(duì)Gif 
  116. header("Content-Type:image/gif"); 
  117. imagegif($this->img); 
  118. }else if(imagetypes() & IMG_WBMP){ 
  119. // 針對(duì) WBMP 
  120. header(′Content-Type: image/vnd.wap.wbmp′); 
  121. imagewbmp($this->img); 
  122. }else
  123. die(′No image support in this PHP server′); 
  124.  
  125. //創(chuàng)建驗(yàn)證碼 
  126. private function createCode(){ 
  127. //默認(rèn)字符串 
  128. $codes="123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"
  129. //生成驗(yàn)證碼 
  130. $code=""
  131. for($i=0;$i<$this->num;$i++){ 
  132. $code.=$codes{rand(0,strlen($codes)-1)}; 
  133. //Vevb.com 
  134. return $code
  135.  
  136. //析構(gòu)函數(shù)用于銷毀圖像資源 
  137. function __destruct(){ 
  138. imagedestroy($this->img); 

這里我使用的畫(huà)字體的函數(shù)是imagettftext()、因?yàn)檫@個(gè)函數(shù)可以自定義字體樣式、從代碼中也能看出來(lái)

傳入的參數(shù)有個(gè)字體文件屬性、如果不喜歡用這個(gè)函數(shù)可以使用imagestring()函數(shù)也行

只不過(guò)個(gè)人覺(jué)得這個(gè)函數(shù)的默認(rèn)字體大小、也不好看、還是自定義字體看著舒服點(diǎn)

調(diào)用驗(yàn)證碼類image_002.php代碼如下:

  1. //開(kāi)啟Sessionsession_start();//引入驗(yàn)證碼類include("vcode.class.php");//創(chuàng)建驗(yàn)證碼類$vcode=new Vcode();//將獲取的驗(yàn)證碼存入到session中$_SESSION[′code′]=$vcode->getCode();//$vcode->set("pointNum",10);//自定義干擾點(diǎn)個(gè)數(shù)//$vcode->set("lineNum",10);//自定義干擾線個(gè)數(shù)//$vcode->set("fontFile","wawa.ttf");//自定義字體文件//輸出圖像$vcode->outImg(); 

HTML代碼如下:

  1. <img src="image_002.php" onclick="this.src=′image_002.php?Math.random()′"/> 

到這里整個(gè)功能就做完了、希望對(duì)一些哥們有用、同時(shí)也當(dāng)自己做個(gè)筆記

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东山县| 五华县| 晋中市| 上栗县| 八宿县| 东源县| 区。| 平原县| 宜都市| 滕州市| 句容市| 汉中市| 桐柏县| 隆子县| 集贤县| 天峻县| 大化| 大新县| 壶关县| 灵璧县| 柳州市| 海安县| 淮北市| 淅川县| 达州市| 吉水县| 淮滨县| 高安市| 邯郸县| 澄迈县| 涿鹿县| 张北县| 鄂托克前旗| 马关县| 临夏市| 苏尼特左旗| 镇康县| 高密市| 襄垣县| 蓬莱市| 荥阳市|