ホームページ  >  記事  >  php教程  >  かなり包括的な PHP 検証コード クラス

かなり包括的な PHP 検証コード クラス

WBOY
WBOYオリジナル
2016-06-21 08:50:531264ブラウズ

PHP100 ビデオ 34 で記述された PHP 検証コード クラスを参照してください。カスタム画像サイズ、文字数、文字タイプ、文字サイズ、画像タイプ、漢字検証コード、干渉線などをサポートしています。詳細なコメント

フォント ファイルは Microsoft Yahei で、検証コード ファイルと同じディレクトリにあり、ファイル名は msyh.ttf で、C:windowsfonts ディレクトリにあり、同じディレクトリにコピーできます。

/**

 * @version Id: imgcode.php 2012-11-30 k

 * @package imgcode.php

 * @Purview デフォルト

 * @link http://www.3oom.com

 */

$img = 新しい img_code();

$img->show();

クラス img_code{

Private $img_type = 'png';//出力画像タイプ png、gif、jpg

Private $line = TRUE;//干渉ラインを追加するかどうか

Private $text = '2';//認証コードの種類: 数字 0、文字 1、漢字 2

プライベート $text_size= 20;//フォント サイズ

Private $length = 4;//文字列の長さ

Private $width = 120;//画像の幅、ピクセル

Private $height = 40;//画像の高さ、ピクセル

プライベート $font_file= 'msyh.ttf';// フォント ファイル

パブリック $img = '';

/*

*写真を作成します

*/

プライベート関数 img_create(){

$this->img = imagecreate($this->width, $this->height);

Imagecolorallocate($this->img, 255,255,255);

}

/*

*写真を表示

*/

パブリック関数 show(){

@session_start();

$this->img_create();

If($this->line){

$this->img_line();

}

$this->img_text();

$this->img_header();

Imagedestroy($im);

exit();

}

/*

* 文字列を生成

*/

プライベート関数 img_text(){

$rand_string = '';

$im = &$this->img;

$fontColor[] = imagecolorallocate($im, 0x15, 0x15, 0x15);

$fontColor[] = imagecolorallocate($im, 0x95, 0x1e, 0x04);

$fontColor[] = imagecolorallocate($im, 0x93, 0x14, 0xa9);

$fontColor[] = imagecolorallocate($im, 0x12, 0x81, 0x0a);

$fontColor[] = imagecolorallocate($im, 0x06, 0x3a, 0xd5);

If($this->text //数字、文字

for($i=0;$ilength;$i++){

If($this->text){

$c=chr(mt_rand(65,90));

}else{

$c=chr(mt_rand(48,57));

}

If( $c=='I' ) $c = 'K';

If( $c=='O' ) $c = 'E';

$rand_string[]=$c;

                                                                                                    

}その他{

//漢字、ここに漢字ファイルをインポートできます

$arr=array('大きい','小さい','もっと','少ない','人','空','水','土','木','火','雲');

for($i=0;$ilength;$i++){

$l = count($arr)-1;

// ファイルエンコーディングは gbk なので変換する必要があります

//$rand_string[] = iconv('gb2312','utf-8',$arr[mt_rand(0,$l)]);

$rand_string[] = $arr[mt_rand(0,$l)];

}

                                                                       

}

$_SESSION['imgcode']=$rand_string;

$_c = count($rand_string);

for($i=0;$i {

If($this->text == 1){

$rand_string[$i] = strtoupper($rand_string[$i]);

}

                                                                         

$c_fontColor = $fontColor[mt_rand(0,4)];

$y = $this->height-($this->height-$this->text_size)/2;

$x = ($this->width-($this->text_size+2)*$this->length)/2;

$y_pos = $i==0 ? $x : $i*($this->text_size+2)+$x;

$c = mt_rand(0, 15);

@imagettftext($im, $this->text_size, $c, $y_pos, $y, $c_fontColor, $this->font_file, $rand_string[$i]);

                                                                       

}

}

/*

* ラインを生成

*/

プライベート関数 img_line(){

$im = &$this->img;

$img_width = $this->width;

$img_height= $this->height;

// 背景の水平線 >

$lineColor1 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);

for($j=3; $j {

Imageline($im, 2, $j, $img_width - 2, $j, $lineColor1);

}

//背景の縦線

$lineColor2 = imagecolorallocate($im, 0xda,0xd9,0xd1);

for($j=2;$j {

Imageline($im, $j, 0, $j+8, $img_height, $lineColor2);

}

// 境界線を描画します

if( $use_boder && $filter_type == 0 )

{

$bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);

Imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $bordercolor);

}

}

/*

* 出力を生成

*/

プライベート関数 img_header(){

header("Pragma:no-cachern");

header("キャッシュコントロール:no-cachern");

header("有効期限:0rn");

If($this->img_type == 'jpg'){

header('Content-type: image/jpeg');

Imagejpeg($this->img);

}else if($this->img_type == 'png'){

header('Content-type: image/png');

Imagepng($this->img);

}その他{

header('Content-type: image/gif');

Imagegif($this->img);

}

}

}

記事の出典: http://www.3oom.com/blog/50.html



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。