php中文驗證碼

 

  1. <?php 
  2. header("content-type: image/png"); 
  3.   class yanzhengma{ 
  4.   public $str="你是我的好朋友"//驗證碼 
  5.   public $geshu=4;//驗證碼個數 
  6.   public $img;//圖片sourcer 
  7.   public function  __construct($geshu){ 
  8.     $this->geshu=$geshu//初始化個數 
  9.    
  10.    public function yanzhengma($x,$y){ 
  11.     //創建一個畫布x表示橫座標,y表示縱座標 
  12.     $this->img=imagecreatetruecolor($x,$y);//創建畫布,返回畫布句柄 
  13.     $rand = strlen($this->str)-2; 
  14.     $rand = $rand/2;//中文佔2個字節 
  15.     $text_color=imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//隨機創建文本的顏色 
  16.     $text_color2=imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//隨機創建背景的顏色 
  17.      
  18.    $text=''
  19.    for($i=0;$i<$this->geshu;$i++){ 
  20.        $rand=mt_rand(0,$rand);//得到驗證碼 
  21.        $text=$text.substr($this->str,$rand*2,2); 
  22.     } 
  23.      
  24.      
  25.     $text=iconv('gb2312''UTF-8'$text);//必須轉換才能用 
  26.     imagefill($this->img, 0, 0, $text_color2);//填充背景 
  27.     imagettftext ($this->img,20,1,10,30,$text_color,"simkai.ttf",$text);//這個函數用於輸出中文 
  28.    //imagestring($img, 30, 40, 40, $text, $text_color);該函數只能用於英文數字 
  29.     imagepng($this->img);//顯示圖片 
  30.     return $text;//返回驗證碼,用於以後session 
  31.      
  32.   }  
  33.    
  34.   public function destory(){ 
  35.     imagedestroy($this->img);//一定要關閉資源 
  36.   } 
  37.    } 
  38.     
  39.    $imgnew yanzhengma(5); 
  40.    $text=$img->yanzhengma(200,40); 
  41.    file_put_contents('1.txt'$text)   ; 
  42.    $img->destory(); 
  43. ?> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章