非常實用的php驗證碼類
來源:易賢網(wǎng) 閱讀:800 次 日期:2016-08-22 15:24:56
溫馨提示:易賢網(wǎng)小編為您整理了“非常實用的php驗證碼類”,方便廣大網(wǎng)友查閱!

本文實例為大家分享了php驗證碼類,供大家參考,具體內(nèi)容如下

<?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='') { 

    //創(chuàng)建圖像背景 

    $this->Img_resouce(); 

    //var_dump($img_resouce); 

    //設(shè)置干擾元素 

    $this->setDistructcolor(); 

    //向圖像中隨機(jī)畫出文本 

    $this->outputtext($fontpath); 

    //輸出圖像 

    $this->outputimage(); 

  } 

  /** 

   * 

   *獲取隨機(jī)創(chuàng)建的驗證碼 

   */

  public function getCheckCode(){ 

  } 

  private function Img_resouce(){ 

    //創(chuàng)建一個真彩圖像 

    $this->img_resouce=imagecreatetruecolor($this->width,$this->height); 

    //隨機(jī)設(shè)置圖像背景 

    $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255)); 

    //填充顏色 

    imagefill($this->img_resouce, 0, 0, $backcolor); 

    //設(shè)置邊框背景 

    $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); 

  } 

?>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:非常實用的php驗證碼類
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)