ホームページ  >  記事  >  バックエンド開発  >  中国語の英数字、カスタム フォントの PHP 検証コードをサポート code_PHP チュートリアル

中国語の英数字、カスタム フォントの PHP 検証コードをサポート code_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:20:18871ブラウズ

コードをコピーします コードは次のとおりです:

/*
* PHP GD Lib ベースの Captcha クラス
* @author Design
* @version 1.0
* @demo
* include('captchaClass.php')
* $captchaDemo=new Captcha ();
* $captchaDemo->createImage();
*/
class Captcha{
//@検証コード画像の高さを定義します
private $height;
private $width;
//@検証コードの文字数を定義します
private $textNum;
//@検証コードの文字の内容を定義します
private $textContent;
//@文字の色を定義します
private $fontColor;
//@ランダムなテキストの色を定義します
private $randFontColor;
//@フォントのサイズを定義します
private $fontFamily;
//@背景の色を定義します
//@ランダムな背景色を定義
private $randBgColor;
//@文字言語を定義
//@干渉点の数を定義
private $noisePoint;
private $noiseLine;
//@歪んでいるかどうかを定義
private $distortion;
//@境界線があるかどうかを定義
/ /@検証コード画像ソースを定義します
private $image;

//@ コンストラクター コンストラクター
$this->textNum=4;
$this->fontSize=16; ->fontFamily='c:windowsfontsSIMYOU.ttf';//中国語を設定 フォントは Linux ディレクトリに変更できます
$this->textLang='en';
$this->noisePoint=30; this->noiseLine=3;
$this->distortion=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;
}

//@歪ませるかどうかを設定
public 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->image= imagecreatetruecolor($this->width,$this->height) ;
if(empty($this->bgColor)){
$this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255)); else{
$this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
}
imagefill($this->image,0,0,$this->randBgColor);
}

//@ランダムな文字を生成
public function randText($type){
$string='';
switch($type){
case 'en':
$str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
for($i=0;$i<$this ->textNum;$i++){
$string=$string. ','.$str[mt_rand(0,29)];
}
break;
for($i=0; $i<$this->textNum;$i++) {
$ string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB))
}
$string =iconv('GB2312','UTF-8',$string); ; //エンコーディングを utf8 に変換します
break;
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->画像,$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;
}

}
?>使用方法:

//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:windowsfontsSTXINGKA.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/325114.html技術記事复制代码 代码如下: ?php /* * PHP GD Lib に基づく Captcha クラス * @author Design * @version 1.0 * @demo * include('captchaClass.php'); * $captchaDemo=新しいキャプチャ(); * $captch...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。