PHP驗證碼類源代碼(字母帶干擾點)

2011-09-28 20:09:19來源:itwhy作者:

這是一個PHP驗證碼類,支持干擾碼、干擾線和傾斜。

這是一個PHP驗證碼類,支持干擾碼、干擾線和傾斜。

PHP驗證碼類生成的樣式圖片:

其實PHP生成驗證碼沒有想象中的難,我自己寫了一個類,記住備忘。以下是這個類可以生成的樣式啦!

驗證碼樣式1 驗證碼樣式2 驗證碼樣式3 驗證碼樣式4

驗證碼樣式5 驗證碼樣式6 驗證碼樣式7 驗證碼樣式8

PHP 驗證碼類代碼:

PHP Code復制內容到剪貼板
  1. <?php   
  2. /**  
  3.  * PHP 驗證碼,支持干擾點、干擾線、傾斜。  
  4.  * 日期:2011-09-23  
  5.  * 作者:www.itwhy.org  
  6.  * 使用:  
  7.  *      $obj = new class_authcode();        //實例化對象,并設置驗證碼圖片的寬、高和驗證碼的長度。  
  8.  *      $obj->$authcode;                    //獲取驗證碼。  
  9.  *      $obj->output();                     //輸出驗證碼圖片。  
  10.  */  
  11. class class_authcode{   
  12.     public  $authcode   = '';                           //驗證碼   
  13.   
  14.     private $width      = '';                           //驗證碼圖片寬   
  15.     private $height     = '';                           //驗證碼圖片高   
  16.     private $len        = '';                           //驗證碼長度   
  17.     private $tilt       = array(-30,30);                //驗證碼傾斜角度   
  18.     private $font       = 'AlteHaasGroteskBold.ttf';    //字體文件   
  19.     private $str        = '';                           //驗證碼基   
  20.     private $im         = '';                           //生成圖片的句柄   
  21.   
  22.     //構造函數(shù),生成驗證碼。   
  23.     function __construct($width=100,$heigh=30,$len=4) {   
  24.         $this->width    = $width;   
  25.         $this->height   = $heigh;   
  26.         $this->len      = $len;   
  27.         $this->str      = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';   
  28.   
  29.         $str_len = strlen($this->str)-1;   
  30.         for ($i=0; $i<$len$i++) {   
  31.             $this->authcode .= $this->str[rand(0,$str_len)];   
  32.         }   
  33.     }   
  34.     //創(chuàng)建圖片   
  35.     private function imagecreate(){   
  36.         $this->im = imagecreatetruecolor($this->width,$this->height);   
  37.     }   
  38.     //干擾顏色   
  39.     private function ext_color() {   
  40.         return imagecolorallocate($this->im,rand(50, 180),rand(50, 180),rand(50, 180));   
  41.     }   
  42.     //創(chuàng)建干擾點   
  43.     private function ext_point() {   
  44.         for ($i=0; $i<$this->width*2; $i++) {   
  45.             imagesetpixel($this->im,rand(1,$this->width-1),rand(1,$this->height-1),$this->ext_color());   
  46.         }   
  47.     }   
  48.     //創(chuàng)建干擾線   
  49.     private function ext_line() {   
  50.         for ($i=0; $i<$this->len; $i++) {   
  51.             $x1 = rand(1,$this->width-1);   
  52.             $y1 = rand(1,$this->height-1);   
  53.             $x2 = rand(1,$this->width-1);   
  54.             $y2 = rand(1,$this->height-1);   
  55.             imageline($this->im,$x1,$y1,$x2,$y2,$this->ext_color());   
  56.         }   
  57.     }   
  58.     //把驗證碼寫入圖片(不能和$this->imgstrfloat()同時使用)   
  59.     private function imgstr() {   
  60.         $old_x = 1;   
  61.         for ($i=0; $i<$this->len; $i++) {   
  62.             $fontsize = rand(2,5);      //字體大小   
  63.             $tmp_1 = $fontsize*2.5;   
  64.             $tmp_2 = $i>0 ? $tmp_1 : 0;   
  65.             $y = rand(1,$this->height/2);   
  66.             $x = rand($old_x+$tmp_2, ($i+1)*($this->width)/$this->len-$tmp_1);   
  67.             $old_x = $x;   
  68.             $color = imagecolorallocate($this->im,rand(200, 255),rand(200, 255),rand(200, 255));   
  69.             imagestring($this->im,$fontsize,$x,$y,$this->authcode[$i],$color);   
  70.         }   
  71.     }   
  72.     //把驗證碼傾斜寫入圖片(不能和$this->imgstr()同時使用)   
  73.     private function imgstrfloat() {   
  74.         $old_x = 1;   
  75.         for ($i=0; $i<$this->len; $i++) {   
  76.             $fontfloat = rand($this->tilt[0],$this->tilt[1]);   
  77.             $fontsize = rand(10,15);        //字體大小   
  78.             $tmp_1 = $i>0 ? $fontsize : 0;   
  79.             $y = rand($fontsize+2, $this->height-2);   
  80.             $x = rand($old_x+$tmp_1+2, ($i+1)*($this->width)/$this->len-$fontsize-2);   
  81.             $old_x = $x;   
  82.             $color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));   
  83.             imagettftext($this->im, $fontsize$fontfloat$x$y$color$this->font, $this->authcode[$i]);   
  84.         }   
  85.     }   
  86.     //輸出圖片   
  87.     function output() {   
  88.         $this->imagecreate();   
  89.         $this->imgstr();   
  90.         //$this->imgstrfloat();   
  91.         $this->ext_point();   
  92.         $this->ext_line();   
  93.         header('content-type:image/png');   
  94.         imagepng($this->im);   
  95.         imagedestroy($this->im);   
  96.     }   
  97. }   
  98. ?>  

原文:http://www.itwhy.org/2011/09-23/1247.html
 

贊助商鏈接: