1. <?php 
  2. header("content-type: p_w_picpath/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=p_w_picpathcreatetruecolor($x,$y);//创建画布,返回画布句柄 
  13.     $rand = strlen($this->str)-2; 
  14.     $rand = $rand/2;//中文占2个字节 
  15.     $text_color=p_w_picpathcolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//随机创建文本的颜色 
  16.     $text_color2=p_w_picpathcolorallocate($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.     p_w_picpathfill($this->img, 0, 0, $text_color2);//填充背景 
  27.     p_w_picpathttftext ($this->img,20,1,10,30,$text_color,"simkai.ttf",$text);//这个函数用于输出中文 
  28.    //p_w_picpathstring($img, 30, 40, 40, $text, $text_color);该函数只能用于英文数字 
  29.     p_w_picpathpng($this->img);//显示图片 
  30.     return $text;//返回验证码,用于以后session 
  31.      
  32.   }  
  33.    
  34.   public function destory(){ 
  35.     p_w_picpathdestroy($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. ?>