首頁 > 後端開發 > php教程 > 一個php驗證碼的封裝類

一個php驗證碼的封裝類

WBOY
發布: 2016-07-25 08:58:47
原創
1040 人瀏覽過
  1. /**

  2. * 驗證碼類別
  3. * edit bbs.it-home.org
  4. */
  5. class ValidationCode {
  6. private $width;private $height;
  7. private $codeNum;
  8. private $image; //影像資源
  9. private $disturbColorNum;
  10. private $checkCode;
  11. function __construct(>private $checkCode;
  12. function __construct(ight =20, $codeNum=4){
  13. $this->width=$width;
  14. $this->height=$height;
  15. $this->codeNum=$codeNum;
  16. $this ->checkCode=$this->createCheckCode();
  17. $number=floor($width*$height/15);
  18. if($number > 240-$codeNum){

  19. $this->disturbColorNum= 240-$codeNum;
  20. }else{
  21. $this->disturbColorNum=$number;
  22. }
  23. }

  24. //透過存取該方法向瀏覽器輸出圖像
  25. function showImage($fontFace=""){
  26. //第一步:建立圖像背景
  27. $this->createImage();
  28. //第二步:設定乾擾元素
  29. $this->setDisturbColor();
  30. //第三個步驟:隨機畫出文字
  31. $this->outputText($fontFace);
  32. //第四步:輸出影像
  33. $this->outputImage();
  34. }
  35. //透過呼叫此方法取得隨機建立的驗證碼字串

  36. function getCheckCode(){
  37. return $this->checkCode;
  38. }
  39. private function createImage(){
  40. //建立映像資源//bbs.it-home.org
  41. $this->image=imagecreatetruecolor($this->width, $this->height);
  42. //隨機背景色
  43. $backColor=imagecolorallocate($this->image, rand(225, 255) , rand(225,255), rand(225, 255));
  44. //為背景增加顏色
  45. imagefill($this->image, 0, 0, $backColor);
  46. //設定邊框顏色
  47. $border=imagecolorallocate($this->image, 0, 0, 0);
  48. //畫出長方形邊框
  49. imagerectangle($this->image, 0, 0, $this-> width-1, $this->height-1, $border);
  50. }
  51. private function setDisturbColor(){
  52. for($i=0; $idisturbColorNum; $i++ ){
  53. $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  54. imagesetpixel($this->image, rand (1, $this->width-2), rand(1, $this->height-2), $color);
  55. }
  56. for($i=0; $i$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
  57. imagearc($this->image, rand (-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
  58. }
  59. }
  60. private function createCheckCode(){
  61. //這裡主要產生隨機碼,從2開始是為了區分1和l
  62. $code="23456789abcdefghijkmnpqrstuvwxyz$code="23456789abcdefghijkmnpqrstuvwxyz$code="23456789abcdefghijkmnpqrstuvwxyzDEWxg ;
  63. for($i=0; $i codeNum; $i++){
  64. $char=$code{rand(0, strlen($code)-1)};
  65. $string.=$char;
  66. }
  67. return $string;
  68. }
  69. private function outputText($fontFace=""){
  70. for($i=0; $icodeNum; $i++){
  71. $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
  72. if( $fontFace==""){
  73. $fontsize=rand(3, 5);
  74. $x=floor($this->width/$this->codeNum)*$i+3;
  75. $y=rand(0, $this->height-15);
  76. imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
  77. }else{
  78. $fontsize=rand(12, 16);
  79. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  80. $y=rand($fontSize+5, $this->height);
  81. imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
  82. }
  83. }
  84. }
  85. //輸出圖象資訊

  86. private function outputImage() {
  87. if(imagetypes() & IMG_GIF){
  88. header("Content-Type:image/gif");
  89. imagepng($this->image);
  90. }else if(imagetypes( ) & IMG_JPG){
  91. header("Content-Type:image/jpeg");
  92. imagepng($this->image); //bbs.it-home.org
  93. }else if(imagetypes () & IMG_PNG){
  94. header("Content-Type:image/png");
  95. imagepng($this->image);
  96. }else if(imagetypes() & IMG_WBMP){
  97. }else if(imagetypes() & IMG_WBMP){
  98. header("Content-Type:image/vnd.wap.wbmp");
  99. imagepng($this->image);
  100. }else{
  101. die("PHP不支援影像建立");
  102. }
  103. }
  104. function __destruct(){
  105. imagedestroy($this->image);
  106. }
  107. }
  108. ?>
  109. }
}
?>

複製程式碼

驗證碼類別的呼叫範例:
  1. session_start();
  2. include "validationcode.class.php";
  3. $code=new ValidationCode(80ationcode.class.php";
$code=new ValidationCode(80ationcode.class. , 20, 4);
$code->showImage(); //輸出到頁面中供註冊或登入使用$_SESSION["code"]=$code->getCheckCode(); //將驗證碼儲存到伺服器?>複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板