ホームページ  >  記事  >  バックエンド開発  >  PHP 中国語英数字検証コード クラス、カスタマイズ可能なフォント_PHP チュートリアル

PHP 中国語英数字検証コード クラス、カスタマイズ可能なフォント_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 14:53:40942ブラウズ

認証コードの問題は、ログインページ、メッセージページ、登録ページなど、実際のプロジェクト開発でよく遭遇します

検証コードの原理は非常に簡単です。もちろん、GD ライブラリを使用してイメージを作成し、必要な干渉コードを追加して、ユーザーが送信するときにセッションをサーバー側に保存する必要があります。セッションが同じかどうかが判断されます。

今回共有された PHP 検証コード クラスは、中国語の文字、英数字をサポートしており、フォント ファイルをカスタマイズすることもできます。誰でも使用するのに十分です。クリップボードにコピーLiehuo.Net Codes引用コンテンツ: [www.bkjia.com] /*
* PHP GD Lib に基づく Captcha クラス
* @author デザイン
* @version 1.0
* @copyright js8.in 2010
* @demo
* include('captchaClass.php'); * $captchaDemo=new Captcha();
* $captchaDemo->createImage();
*/
class Captcha{
//@検証コード画像の高さを定義します
private $height;検証コード画像の
private $width;
//@検証コードの文字数を定義
private $textNum;
//@検証コードの文字の内容を定義
private $textContent;
private $fontColor;
//@ ランダムなテキストの色を定義します
private $fontSize;
//@フォントを定義します
private $fontFamily;背景色
private $bgColor;
//@ ランダムな背景色を定義します
private $textLang;
//@干渉点の数を定義します
private $noisePoint; //@干渉線の数を定義します
private $noiseLine; //@歪ませるかどうかを定義します
private $distortion; //@歪んだ画像ソースを定義します
private $distortionImage; border
private $showBorder;
//@検証コード画像ソースを定義

//@Constructor コンストラクター
$this->textNum=4; ;fontSize=16;
$this->fontFamily='c:\windows\ fontsSIMYOU.ttf';//中国語フォントを設定します。Linux ディレクトリに変更できます
$this->textLang='en';
$this->noisePoint=30;
$this->distortion=false;
$this->showBorder=false; //@画像の幅を設定します
public function setWidth($w){
$this-> width=$w;
}

//@画像の高さを設定します
public function setHeight($h){
$this ->height=$h;
}

//@文字数を設定する
public function setTextNumber($textN){
$this->textNum=$textN;

//@文字の色を設定する
public function setFontColor($fc){
$this->fontColor=sscanf($fc,'# %2x%2x%2x')
}

//@フォントサイズを設定します
public function setFontSize( $n){
$this->fontSize=$n;
}

//@フォントを設定する
public function setFontFamily($ffUrl){
$this->fontFamily=$ffUrl;

// @文字の言語を設定します
public function setTextLang($lang){
$this->textLang=$lang;
}

//@画像の背景を設定します
public function setBgColor($bc){
$this-> bgColor=sscanf($bc,'#%2x%2x%2x');
}

//@干渉点を設定する
public function setNoisePoint($n){
$this->noisePoint=$ n;
}

//@干渉ラインの数を設定
public function setNoiseLine($n){
$this->noiseLine=$ n;
}

//@境界線を表示するかどうかを設定
function setDistortion($b){
$this->distortion=$b;
}

//@枠線を表示するかどうかを設定
public function setShowBorder($border ){
$this->showBorder=$border ;
}

//@初期化検証コード画像
public function initImage(){
if(empty($this->width)){$this->width=floor($this->fontSize*1.3 )*$this->textNum+10;}
if(empty($this->height)){$this->height=$this->fontSize*2;}
$this->image =imagecreatetruecolor($this->幅,$this->高さ);
if(empty($this->bgColor)){
$this->randBgColor=imagecolorallocate($this->image,mt_rand; (100,255),mt_rand(100,255),mt_rand(100,255));
}else{
$this->randBgColor=imagecolorallocate($this->gt;image,$this ->bgColor[0],$this-> ;bgColor[1],$this->bgColor[2]);
}
imagefill($this->image,0,0,$this->randBgColor);
}

//@ランダムを生成文字
パブリック関数 randText($type){
$string='';
switch($type){
case 'en':
$str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
for($i=0;$i<$ this->textNum;$i++){
$string=$string.','.$str[mt_rand(0,29)];
case 'cn':
for($i=0) ;$i<$this->textNum;$i++) {
$string=$string.','.chr(rand(0xB0,0xCC)).chr( rand(0xA1,0xBB)); string=iconv('GB2312','UTF-8',$string); //エンコーディングを utf8 に変換します
}
return substr($string,1) ;//@输出文字到验证码
public function createText(){
$textArray=explode(',',$this->randText($this->textLang));
$this->textContent=join('',$textArray);
if(empty($this->fontColor)){
$this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}else{
$this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2] );
}
for($i=0;$itextNum;$i++){
$angle=mt_rand(-1,1)*mt_rand(1,20);
imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$ this->randFontColor,$this->fontFamily,$textArray[$i]);
}
}

//@生成干扰点
public function createNoisePoint(){
for($i=0;$i<$this->noisePoint;$i++){
$pointColor=imagecolorallocate($this- >画像,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->image,mt_rand(0,$this->幅),mt_rand(0,$this->高さ),$pointColor);
}

}

//@产生干扰線
public function createNoiseLine(){
for($i=0;$i<$this->noiseLine;$i++) {
$lineColor=imagecolorallocate($this) ->画像,mt_rand(0,255),mt_rand(0,255),20);
imageline($this->image,0,mt_rand(0,$this->幅),$this->幅,mt_rand(0,$this->高さ),$lineColor);
}
}

//@扭曲文字
public functiondistortionText(){
$this->distortionImage=imagecreatetruecolor($this->width,$this->height);
imagefill($this->distortionImage,0,0,$this->randBgColor);
for($x=0;$x幅;$x++){
for($y=0;$y高さ;$y++){
$rgbColor=imagecolorat( $this->画像,$x,$y);
imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->高さ*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
}
}
$this->image=$this->歪み画像;
}

//@生成验证码图片
public function createImage(){
$this->initImage(); // 基本画像を作成
$this->createText(); //输出验证码文字
if($this->distortion){$this->distortionText();} //扭曲文字
$this->createNoisePoint(); //産干扰点
$this->createNoiseLine(); //産干扰線
if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this- >randFontColor);} //追加边框
imagepng($this->image);
imagedestroy($this->image);
if($this->distortion){imagedestroy($this->$distortionImage);}
return $this->textContent;
}

}

使用方法:

クリップボードにコピーLiehuo.Net Codes引用内容:[www.bkjia.com] //session_start();
header("Content-type:image/png");
include('captcha5_class.php');
$captcha5=新しいキャプチャ();

//@设置验证码宽度
//$captcha5->setWidth(200);

//@设置验证码高さ
//$captcha5->setHeight(50);

//@设置文字数
$captcha5->setTextNumber(5);

//@设置字符颜色
//$captcha5->setFontColor('#ff9900');

//@设置字号大小
//$captcha5->setFontSize(25);

//@设置文字体
$captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF');

//@设置语言
$captcha5->setTextLang('cn');

//@设設置背景颜色
//$captcha5->setBgColor('#000000');

//@设置干扰点数
//$captcha5->setNoisePoint(600);

//@设置干扰線数
//$captcha5->setNoiseLine(10);

//@设置是否扭曲
//$captcha5->setDistortion(true);

//@設置かどうか表示边框
$captcha5->setShowBorder(true);

//出验证码
$code=$captcha5->createImage();
//$_SESSION['captchaCode']['content']=$code;
//$_SESSION['captchaCode']['time']=microtime();
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/364723.html技術記事実際のプロジェクト開発では、ログイン ページ、メッセージ ページ、登録ページなどの検証コードの問題がよく発生します。検証コードの原理は非常に単純です。画像を作成するには GD ライブラリを使用する必要があります。 .
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。