+ -
当前位置:首页 → 问答吧 → 根据老师的我写的一个验证码图片类

根据老师的我写的一个验证码图片类

时间:2010-01-31

来源:互联网

复制代码
  1. class Verif{
  2.     
  3.         private $width;            //验证码图片的宽度
  4.         private $height;        //验证码图片的高度
  5.         private $codeNum;        //验证码的个数
  6.         private $image;            //创建验证码图片资源变量
  7.         private $checkCode;        //输出的验证码的字符串
  8.         /*
  9.             功能:初始化构造函数,为主要参数赋值
  10.         */
  11.         function __construct($width=400,$height=40,$codeNum=4){
  12.         
  13.             $this->width=$width;
  14.             $this->height=$height;
  15.             $this->codeNum=$codeNum;
  16.             $this->checkCode=$this->createCheckCode();
  17.         
  18.         }
  19.         
  20.         function showImage(){
  21.             
  22.             $this->setImage();
  23.             $this->outPutText();
  24.             $this-> setDisturbColor();
  25.             $this->outPutImage();
  26.             
  27.         }
  28.         function getCheckCode(){
  29.         
  30.             return $this->checkCode;
  31.         
  32.         }
  33.         /*
  34.             功能:生成图片
  35.         */
  36.         private function setImage(){
  37.         
  38.             $this->image=imagecreatetruecolor($this->width,$this->height);
  39.             $bg_color=imagecolorallocate($this->image,255,255,255);
  40.             $boder_color=imagecolorallocate($this->image,0,0,0);
  41.             imagefill($this->image,0,0,$bg_color);
  42.             imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$boder_color);
  43.         
  44.         }
  45.         
  46.         private function createCheckCode(){
  47.         
  48.             $arrCode=array('鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪');
  49.             $checkCode='';
  50.             for($i=0;$i<$this->codeNum;$i++){
  51.             
  52.                 $checkCode.=$arrCode[rand(0,count($arrCode)-1)];
  53.             
  54.             }
  55.             return $checkCode;
  56.         
  57.         }
  58.         
  59.         private function outPutText(){
  60.         
  61.             $text=iconv('gb2312','utf-8',$this->checkCode);
  62.             $font='C:/WINDOWS/FONTS/simkai.ttf';
  63.             for($i=0;$i<$this->codeNum;$i++){
  64.                 
  65.                 $text_color=imagecolorallocate($this->image,rand(0,255),rand(0,127),rand(0,255));
  66.                 $x=floor($this->width/$this->codeNum)*$i+3;
  67.                 $y=rand($this->height-5,$this->height-20);
  68.                 $char=$text[$i*3].$text[$i*3+1].$text[$i*3+2];
  69.                 imagettftext($this->image,20,0,$x,$y,$text_color,$font,$char);
  70.             
  71.             }
  72.         
  73.         }
  74.         /*
  75.             功能:生成干扰线
  76.         */
  77.         private function setDisturbColor(){
  78.         
  79.             for($i=0;$i<=5;$i++){
  80.             
  81.                 $line_color=imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
  82.                 imageline($this->image,rand(0,$this->width/2),rand(0,$this->height),rand($this->width/2,$this->width),rand(0,$this->height),$line_color);
  83.             
  84.             }
  85.             for($a=0;$a<=800;$a++){
  86.             
  87.                 $point_color=imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
  88.                 imagesetpixel($this->image,rand(0,$this->width),rand(0,$this->height),$point_color);
  89.             
  90.             }
  91.         
  92.         }
  93.         private function outPutImage(){
  94.         
  95.             if(imagetypes() & IMG_GIF){
  96.             
  97.                 header('Content-type:image/gif');
  98.                 imagegif($this->image);
  99.             
  100.             }elseif(imagetypes() & IMG_JPG){
  101.             
  102.                 header('Content-type:image/jpeg');
  103.                 imagejpeg($this->image);
  104.             
  105.             }elseif(imagetypes() & IMG_PNG){
  106.             
  107.                 header('Content-type:image/png');
  108.                 imagepng($this->image);
  109.             
  110.             }elseif(imagetypes() & IMG_WBMP){
  111.             
  112.                 header('Content-type:image/vnd.wap.wbmp');
  113.                 imagewbmp($this->image);
  114.             
  115.             }else{
  116.             
  117.                 die('PHP不支持图像创建!');
  118.             
  119.             }
  120.         
  121.         }
  122.         /*
  123.             功能:当对象结束之前销毁资源释放内存
  124.         */
  125.         function __destruct(){
  126.         
  127.             imagedestroy($this->image);
  128.         
  129.         }
  130.     
  131.     }

作者: birth0529   发布时间: 2010-01-31

沙发…

作者: php100.org   发布时间: 2010-02-01

.......................

作者: charlesma   发布时间: 2010-02-02