ホームページ >バックエンド開発 >PHPチュートリアル >最新かつ完全なPHP検証コード生成コード(推奨)の詳細説明_phpサンプル
1.0 まず、コードを見てください
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
それでは、上記のコードを結合して、上で使用した関数を分析します。
① Imagecreatetruecolor();
imagecreatetruecolor — 新しいトゥルー カラー イメージを作成します (とても長く感じますが、実際には image/create/true/color を注意深く見ると簡単に覚えられます。トゥルー カラー イメージとは何ですか? 続きを読んでください)
resource imagecreatetruecolor ( int $width , int $height )
imagecreatetruecolor() と imagecreate() の両方の関数がキャンバスを作成できます
resource imagecreate ( int $x_size , int $y_size )
imagecreatetruecolor() は、サイズ x と y の黒い画像を作成します (デフォルトは [トゥルー カラー イメージと呼ばれる場合でも] 黒です)。背景色を変更したい場合は、
を行う必要があります。塗りつぶしカラー関数 imagefill($img,0,0,$color) を使用します。
imagecreate は新しい空の画像リソースを作成し、imagecolorAllocate() を使用して背景色を追加します
上記の 2 つの関数は、同じ関数の 2 つのメソッドにすぎません
② imagecolorallocate();
imagecolorallocate — 画像に色を割り当てる
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
色は赤、緑、青の組み合わせです。これらのパラメータは 0 ~ 255 の整数、または 16 進数の 0x00 ~ 0xFF です。
③ mt_rand();
mt_rand — より良い乱数を生成する
int mt_rand ( int $min , int $max )
$min オプション、返される最小値 (デフォルト: 0) $max オプション、返される最大値 (デフォルト: mt_getrandmax())
これは、0 ~ 255 の任意の値で背景色をランダムに生成するために使用されます。そのため、ページを更新してもキャンバスの背景色が異なります。レンダリング:
2.0 内部に干渉線と干渉点を作成し始めます。検証画像が数秒で認識されないようにする
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 //添加干扰线,并循环次,背景颜色随机 for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干扰点,并循环次,背景颜色随机 for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
機能分析:
① imageline();
イメージライン — 線分を描画します
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() は、イメージ画像上に座標 x1,y1 から x2,y2 (画像の左上隅が 0,0) までの線分をカラー color で描画します。
imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor); これは、canvas $img の座標 x1, y1 から x2 までを意味します。 y2ランダム
② imagesetpixel();
imagesetpixel—単一ピクセルを描画します
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() は、カラー color を使用して、イメージ image 内の x、y 座標 (イメージの左上隅が 0, 0) に点を描画します。
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具体含义同上
レンダリング:
3.0 検証英数字を追加
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 //添加干扰线,并循环次,背景颜色随机 for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干扰点,并循环次,背景颜色随机 for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } //添加需要验证的字母或者数字 $rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到验证的一些字母和数字 $str_arr = array(); //命名一个数组 for($i = ;$i<;$i++){ //循环次,就是有四个随机的字母或者数字 $pos = mt_rand(,strlen($rand_str)-); $str_arr[] = $rand_str[$pos];//临时交换 } $x_start=/;//单个字符X轴位置 foreach ($str_arr as $key) { $fontcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagettftext($img, , mt_rand(-,), $x_start, /, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key); $x_start +=;//遍历后单个字符沿X轴 + } imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
機能:
imagettftext();
imagettftext — TrueType フォントを使用して画像にテキストを書き込みます
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
以下のコードを分析します:
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$img----------キャンバス
25----------フォントのサイズ。
mt_rand(-15,15)----------角度システムで表される角度。0 度は、テキストが左から右に読まれることを意味します。値が大きいほど反時計回りの回転を示します。たとえば、90 度は下から上に読むテキストを表します。 (フォントの角度の問題です)
$x_start----------簡単に言うと、キャラクターのX軸の位置です
50/2----------文字の高さ
$fontcolor----------文字色
"C:/Windows/Fonts/Verdana.TTF"----------文字フォントスタイルのパス
$key----------次の文字を走査します
効果:
上記の内容は、この記事で紹介されている最新かつ最も完全な PHP コード生成および運用検証コードの詳細 (推奨) の全体的な説明です。皆様のお役に立てれば幸いです。