PHP實(shí)現(xiàn)適用于自定義的驗(yàn)證碼類(lèi)
本文實(shí)例為大家分享了PHP驗(yàn)證碼類(lèi),利用對(duì)象來(lái)實(shí)現(xiàn)的驗(yàn)證碼類(lèi),供大家參考,具體內(nèi)容如下
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Class Image{
private $img;
public $width = 85;
public $height = 25;
public $code;
public $code_len = 4;
public $code_str = "329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK";
public $bg_color = '#DCDCDC';
public $font_size = 16;
public $font = 'font.ttf';
public $font_color = '#000000';
//創(chuàng)建驗(yàn)證碼餓字符創(chuàng)
public function create_code(){
$code = '';
for( $i=0;$i<$this->code_len;$i++ ){
$code .= $this->code_str[mt_rand(0, strlen($this->code_str)-1)];
}
return $this->code = $code;
}
//輸出圖像
public function getImage(){
$w = $this->width;
$h = $this->height;
$bg_color = $this->bg_color;
$img = imagecreatetruecolor($w, $h);
$bg_color = imagecolorallocate($img,
hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));
imagefill($img, 0, 0, $bg_color);
$this->img = $img;
$this->create_font();
$this->create_pix();
$this->show_code();
}
//寫(xiě)入驗(yàn)證碼
public function create_font(){
$this->create_code();
$color = $this->font_color;
$font_color = imagecolorallocate($this->img, hexdec(substr($color,1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color,5,2)));
$x = $this->width/$this->code_len;
for( $i=0;$i<$this->code_len;$i++ ){
$txt_color = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0, 150), mt_rand(0, 200));
imagettftext($this->img, $this->font_size, mt_rand(-30, 30), $x*$i+mt_rand(3, 6), mt_rand($this->height/1.2, $this->height), $txt_color, $this->font , $this->code[$i]);
//imagestring($this->img, $this->font_size, $x*$i+mt_rand(3, 6),mt_rand(0, $this->height/4) , $this->code[$i], $font_color);
}
$this->font_color = $font_color;
}
//畫(huà)干擾線(xiàn)
public function create_pix(){
$pix_color= $this->font_color;
for($i=0;$i<100;$i++){
imagesetpixel($this->img, mt_rand(0, $this->width),mt_rand(0, $this->height), $pix_color);
}
for($j=0;$j<4;$j++){
imagesetthickness($this->img, mt_rand(1, 2));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
}
//得到驗(yàn)證碼
public function getCode(){
return strtoupper($this->code);
}
//輸出驗(yàn)證碼
private function show_code(){
header("Content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
}
效果圖:

精彩專(zhuān)題分享:ASP.NET驗(yàn)證碼大全 PHP驗(yàn)證碼大全 java驗(yàn)證碼大全
以上就是使用對(duì)象編寫(xiě)的驗(yàn)證碼類(lèi)的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助。
- 一個(gè)漂亮的php驗(yàn)證碼類(lèi)(分享)
- PHP驗(yàn)證碼類(lèi)代碼( 最新修改,完全定制化! )
- 一個(gè)經(jīng)典的PHP驗(yàn)證碼類(lèi)分享
- 分享一個(gè)漂亮的php驗(yàn)證碼類(lèi)
- 非常實(shí)用的php驗(yàn)證碼類(lèi)
- PHP實(shí)現(xiàn)簡(jiǎn)單實(shí)用的驗(yàn)證碼類(lèi)
- 一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類(lèi) 附調(diào)用方法
- PHP驗(yàn)證碼類(lèi)ValidateCode解析
- 一個(gè)簡(jiǎn)單安全的PHP驗(yàn)證碼類(lèi)、PHP驗(yàn)證碼
- PHP編寫(xiě)的圖片驗(yàn)證碼類(lèi)文件分享
- 一個(gè)實(shí)用的php驗(yàn)證碼類(lèi)
相關(guān)文章
PHP實(shí)現(xiàn)統(tǒng)計(jì)在線(xiàn)人數(shù)功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)統(tǒng)計(jì)在線(xiàn)人數(shù)功能,結(jié)合實(shí)例形式分析了php基于session對(duì)象計(jì)算與統(tǒng)計(jì)在線(xiàn)人數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例分享
以下是對(duì)用shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例進(jìn)行了分析介紹,需要的朋友可以參考下2013-07-07
深入分析使用mysql_fetch_object()以對(duì)象的形式返回查詢(xún)結(jié)果
本篇文章是對(duì)使用mysql_fetch_object()以對(duì)象的形式返回查詢(xún)結(jié)果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php基于PDO實(shí)現(xiàn)功能強(qiáng)大的MYSQL封裝類(lèi)實(shí)例
這篇文章主要介紹了php基于PDO實(shí)現(xiàn)功能強(qiáng)大的MYSQL封裝類(lèi),結(jié)合完整實(shí)例形式分析了php基于pdo實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)連接、增刪改查、事務(wù)等操作的方法,需要的朋友可以參考下2017-02-02
php實(shí)現(xiàn)異步將遠(yuǎn)程鏈接上內(nèi)容(圖片或內(nèi)容)寫(xiě)到本地的方法
這篇文章主要介紹了php實(shí)現(xiàn)異步將遠(yuǎn)程鏈接上內(nèi)容(圖片或內(nèi)容)寫(xiě)到本地的方法,涉及php基于curl進(jìn)行遠(yuǎn)程文件傳輸?shù)南嚓P(guān)操作技巧,需要的朋友可以參考下2016-11-11
php自定義函數(shù)br2nl實(shí)現(xiàn)將html中br換行符轉(zhuǎn)換為文本輸入中換行符的方法【與函數(shù)nl2br功能相反】
這篇文章主要介紹了php自定義函數(shù)br2nl實(shí)現(xiàn)將html中br換行符轉(zhuǎn)換為文本輸入中換行符的方法,具有與函數(shù)nl2br相反的功能,并附帶了相應(yīng)的JS實(shí)現(xiàn)方法,需要的朋友可以參考下2017-02-02
解析php中的fopen()函數(shù)用打開(kāi)文件模式說(shuō)明
本篇文章是對(duì)php中的fopen()函數(shù)用打開(kāi)文件模式說(shuō)明進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
基于PHP的cURL快速入門(mén)教程 (小偷采集程序)
cURL 是一個(gè)利用URL語(yǔ)法規(guī)定來(lái)傳輸文件和數(shù)據(jù)的工具,支持很多協(xié)議,如HTTP、FTP、TELNET等。很多小偷程序都是使用這個(gè)函數(shù)。2011-06-06

