ホームページ  >  記事  >  バックエンド開発  >  PHPで簡単な漢字検証コードを実装するというアイデア

PHPで簡単な漢字検証コードを実装するというアイデア

藏色散人
藏色散人転載
2019-09-25 09:17:062309ブラウズ

現在、漢字の認証コードを使用する Web サイトが増えていますが、これにより人々の親密さが増すだけでなく、機械クラッキングの難易度も高まります。

背景キャンバスの作成

$image = imagecreatetruecolor(200, 60);
$background = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background);

干渉点の描画

for ($i=0; $i < 300; $i++) { 
    $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240));
    $pixX = rand(10, 190);
    $pixY = rand(5, 55);
    imagesetpixel($image, $pixX, $pixY, $pixColor);
}

干渉線の描画

//4条水平线
for ($i=0; $i < 5; $i++) { 
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = 0;
    $lineX2 = 300;
    $lineY1 = ($i + 1) * 12;
    $lineY2 = ($i + 1) * 12;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
//10条垂直线
for ($i=0; $i < 30; $i++) { 
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = ($i + 1) * 10;
    $lineX2 = ($i + 1) * 10;
    $lineY1 = 0;
    $lineY2 = 60;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}

中国語の文字を描画する

$text = array(&#39;栀&#39;, &#39;子&#39;, &#39;花&#39;, &#39;开&#39;);
for ($i=0; $i < 4; $i++) {
    $textColor = imagecolorallocate($image, rand(20, 100), rand(20, 100), rand(20, 100));
    $textX = $i * 50 + 10;
    $textY = rand(40, 60);
    imagettftext($image, 30, rand(20, 50), $textX, $textY, $textColor, "/Library/Fonts/华文仿宋.ttf", $text[$i]);
}

ここで注意してください、フォント ファイルは中国語をサポートする必要があります

エンコーディングは utf-8 を使用する必要があります。gbk 中国語を変換することを覚えていますか? [iconv 関数が役立ちます]

出力画像

header("Content-Type:image/png");
imagepng($image);

リソースの破棄

imagedestroy($image);

大まかな処理が終わったら、次のことを行います。それを実行すると、中国語の認証コードが表示されます。もちろん、Web サイトを使用する場合は、漢字データベースのシードがあり、そこから特定の数の漢字がランダムに選択されて表示され、最終的に検証のためにセッションに記録されます。 。

以上がPHPで簡単な漢字検証コードを実装するというアイデアの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はsegmentfault.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。