php驗證碼產生器程式碼範例

來源:互聯網
上載者:User
驗證碼產生器是一款操作簡單,使用方便,功能實用的驗證碼產生工具,使用者可以使用該工具輕鬆產生驗證碼。

  驗證碼產生器 特色介紹:
  1、驗證碼長度:產生驗證碼的字元個數,預設為4;
  2、背景點密度:即在背景上產生N個點,預設為50;
  3、字元旋轉角度:字元垂直為0度,即正常放置為0度,向左向右進行旋轉,預設為55;
  4、驗證:即驗證字元是否正確;
  5.如果路徑為空白,不產生圖片資訊。如果選擇路徑,必須選擇圖片格式;
  6.看不清楚,換一張:當選擇路徑和圖片格式後,點擊一次,產生一張圖片;
  7.產生的圖片資訊:是以驗證內容為檔案名稱。

這篇文章主要為大家詳細介紹了php驗證碼產生器的相關代碼,具有一定的參考價值,感興趣的小夥伴們可以參考一下

這段代碼運行在WINDOS伺服器平台

<?php$iC = new idCode(5,60,30);$iC->createPNG();class idCode{  private $words = array('a','b',  'c','d','e','f','g','h','i','j','k','l',  'm','n','o','p','q','r','s','t','u','v',  'w','x','y','z','A','B','C','D','E','F',  'G','H','I','J','K','L','M','N','O','P',  'Q','R','S','T','U','V','W','X','Y','Z',  '0','1','2','3','4','5','6','7','8','9');  private $fonts;  private $count;//驗證碼字元數  private $height;  private $width;  private $path = '..\myfolder\fonts';  private $keys;  //建構函式  public function construct($count,$width,$height){    $this->count = $count;    $this->getFonts();    $this->height = $height;    $this->width = $width;  }  private function getFonts(){    $dir = dir($this->path);    while(false !== ($file = $dir->read())){        if($file != '.' && $file != '..'){          $this->fonts[count($this->fonts)] = basename($file);        }    }    $dir->close();  }  private function createKeys(){    for($i = 0;$i < $this->count;$i++){      $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];      //使用字型路徑標識      $this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];    }  }  public function createPNG(){    $this->createKeys();    //建立畫布以及顏色塊兒    $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px    $grey = imagecolorallocate($bg,155,155,155);    $blue = imagecolorallocate($bg,0x00,0x00,0xff);    //填充背景    imagefill($bg,0,0,$grey);    //添加字元    $pwidth = $this->width/$this->count;    $x;$y;    for($i = 0;$i < $this->count;$i++){      $rotation = rand(-40,40);//偏轉角度±40°      $fontsize = 33;      $width_txt;      $height_txt;      do{        $fontsize--;        $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);        $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上        $height_txt = $bbox[7] - $bbox[1];      }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));      $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));      $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x座標基本位置      $y = $this->height/2 - $height_txt/2;      imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);    }    //繪製幹擾線    //根據字型酌情增加幹擾線    imageline($bg,0,15,40,10,$blue);    //映像輸出標頭檔    header('Content-type:image/png');    //輸出png映像    imagepng($bg);    //清除緩衝資源    imagedestroy($bg);  }  public function checkKeys($input){    if(count($input)!=$this->count){      return 'ERROR:長度不正確.';    }else{      for($i=0;$i < $this->count;$i++){        //0 o O I l 1 校準,根據所選擇的字型確定是否需要手動校準        if($input[$i] != $this->keys[$i]['char']){          return 'SUCCESS.';        }else{          return 'ERROR:請輸入正確驗證碼.';        }      }    }  }}?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.