欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

非常实用的php验证码类

程序员文章站 2024-04-02 12:15:34
本文实例为大家分享了php验证码类,供大家参考,具体内容如下

本文实例为大家分享了php验证码类,供大家参考,具体内容如下

<?php 
/** 
 * 
 * @author administrator 
 * 
 */ 
class validatecode{ 
   
  private $width; 
  private $height; 
  private $codenum; 
  private $img_resouce; 
  private $disturbcolornum; 
  private $checkcode; 
   
  function __construct($width=80,$height=20,$codenum=4) { 
    $this->width=$width; 
    $this->height=$height; 
    $this->codenum=$codenum; 
    $this->checkcode=$this->createcheckcode(); 
    $number=floor($width*$height/25); 
    if ($number>240-$codenum) { 
      $this->disturbcolornum=240-$codenum; 
    }else{ 
      $this->disturbcolornum=$number; 
    } 
  } 
   
  public function showimage($fontpath='') { 
    //创建图像背景 
    $this->img_resouce(); 
    //var_dump($img_resouce); 
    //设置干扰元素 
    $this->setdistructcolor(); 
    //向图像中随机画出文本 
    $this->outputtext($fontpath); 
    //输出图像 
    $this->outputimage(); 
  } 
  /** 
   * 
   *获取随机创建的验证码 
   */ 
  public function getcheckcode(){ 
     
  } 
  private function img_resouce(){ 
    //创建一个真彩图像 
    $this->img_resouce=imagecreatetruecolor($this->width,$this->height); 
    //随机设置图像背景 
    $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255)); 
    //填充颜色 
    imagefill($this->img_resouce, 0, 0, $backcolor); 
    //设置边框背景 
    $border=imagecolorallocate($this->img_resouce, 0,0,0); 
    //画一个矩形 
    imagerectangle($this->img_resouce,0,0,$this->width-1,$this->height-1,$border); 
  } 
  private function setdistructcolor(){ 
    //绘画干扰点 
    for ($i = 0; $i <$this->disturbcolornum; $i++) { 
       
      imagesetpixel($this->img_resouce, rand(1, $this->width-2), rand(1, $this->height-2), rand(0,255)); 
    } 
     
    //绘画干扰线 
    for ($j = 0; $j <3; $j++) { 
      $linecolor=imagecolorallocate($this->img_resouce,rand(0,255),rand(0,255),rand(0,255)); 
      imagearc($this->img_resouce, rand(0,$this->width), rand(0,$this->height), 
       rand(10, 225), rand(20, 150), 
       55, 44, $linecolor); 
    } 
  } 
  private function createcheckcode(){ 
    $code='23456789abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz'; 
    $string=''; 
    for ($i = 0; $i < $this->codenum; $i++) { 
       
      $char=$code{rand(0, strlen($code)-1)}; 
      $string.=$char; 
    } 
    return $string; 
  } 
  private function outputtext($fontpath=''){ 
    for ($i = 0; $i < $this->codenum; $i++) { 
      $fontcolor=imagecolorallocate($this->img_resouce, rand(0,128), rand(0, 128), rand(0, 128)); 
      if ($fontpath=='') { 
         
         $fontsize=rand(3, 5); 
         $x=floor($this->width/$this->codenum)*$i+3; 
         $y=rand(0, $this->height-20); 
         imagechar($this->img_resouce, $fontsize, $x, $y, $this->checkcode{$i}, $fontcolor); 
    }else{ 
         $fontsize=rand(12, 16); 
         $x=floor(($this->width-8)/$this->codenum)*$i+8; 
         $y=rand($fontsize, $this->height-15); 
         imagettftext($this->img_resouce,$fontsize,rand(-45,45),$x,$y,$fontcolor,fontpath,$this->checkcode{$i}); 
       } 
    } 
  } 
  private function outputimage() { 
     
    if (imagetypes() & img_gif) { 
      header("content-type: image/gif"); 
      imagegif($this->img_resouce); 
    }else if(imagetypes() & img_jpeg) { 
      header("content-type: image/jpeg"); 
      imagejpeg($this->img_resouce); 
    }else if(imagetypes() & img_png) { 
      header("content-type: image/png"); 
      imagepng($this->img_resouce); 
    }else { 
      echo "php不支持的类型"; 
    } 
     
     
  } 
  private function __destruct(){ 
     
    imagedestroy($this->img_resouce); 
  } 
} 
?>

以上就是本文的全部内容,希望对大家的学习有所帮助。