class Captcha{
//驗證碼寬
private $width;
//驗證碼高度
private $height;
//驗證碼長度
private $len;
//驗證碼范圍
private $str = "abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
//字體地址
private $font_dir = 'F:\phpStudy\PHPTutorial\WWW\php\code\4.ttf';
//驗證碼資源
private $img;
//驗證碼字符串
private $code;
public function __construct($config=['width'=>200,'height'=>50,'len'=>4]){
$this->width=$config['width'];
$this->height=$config['height'];
$this->len=$config['len'];
}
public function entry(){
$this->createBg();
$this->getCode();
$this->setCode();
$this->setLine();
$this->setDot();
$this->setBg();
}
//生成畫布
private function createBg(){
$this->img = imagecreate($this->width,$this->height);
//分配驗證碼的背景顏色
imagecolorallocate($this->img,255,255,255);
}
//生成隨機字符串
private function getCode(){
for($i=0;$i<$this->len;$i++){
$key = mt_rand(0,strlen($this->str)-1);
$this->code .= $this->str[$key];
}
$this->setSession();
}
//把驗證碼存入session之后和用戶輸入的驗證碼做比較
private function setSession(){
session_start();
$_SESSION['code']=strtolower($this->code);
}
//輸出驗證碼到畫布
private function setCode(){
for($i=0;$i<$this->len;$i++){
$font_color = imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$x = ($this->width/$this->len)*$i+5;
$fontSize = mt_rand(18,28);
$fontHeight=imagefontheight($fontSize);
$y= mt_rand($fontHeight+5,$this->height);
imagettftext($this->img, $fontSize, mt_rand(-25,25), $x, $y, $font_color, $this->font_dir,$this->code[$i]);
}
}
//畫干擾線
private function setLine(){
for ($i=0; $i <$this->len ; $i++) {
$line_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($this->img, mt_rand(0,$this->width/$this->len), mt_rand(0,$this->height), mt_rand($this->width-$this->width/$this->len,$this->width), mt_rand(0,$this->height), $line_color);
}
}
//畫干擾點到畫布
private function setDot(){
for ($i=0; $i < $this->width ; $i++) {
$dot_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $dot_color);
}
}
//輸出成圖像
private function setBg(){
ob_end_clean();
header('content-type:image/jpeg');
imagejpeg($this->img);
}
public function __destruct(){
imagedestroy($this->img);
}
}
//輸出驗證碼
$captcha = new Captcha();
$captcha->entry();
任城網站建設公司創新互聯公司,任城網站設計制作,有大型網站制作公司豐富經驗。已為任城近1000家提供企業網站建設服務。企業網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的任城做網站的公司定做!
分享題目:PHP驗證碼類
文章分享:http://vcdvsql.cn/article30/gdgcso.html
成都網站建設公司_創新互聯,為您提供網站營銷、域名注冊、網站制作、響應式網站、網站改版、ChatGPT
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯