PHP 实现简易的汉字验证码的思路-php教程

资源魔 36 0
如今愈来愈多的网站都开端应用汉字验证码了,既添加了咱们国人的亲切感,同时也添加了机械破解的难度,这里我就简略粗犷的说一下。

创立布景画布

$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('栀', '子', '花', '开');
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);

通过粗略的搞吧搞吧,中文验证码也就显示进去了,当然普通网站应用的时分会有一个汉字库种子,从外面随机掏出特定个数的汉字显示,最初就是记载到session进行验证了。

以上就是PHP 完成繁难的汉字验证码的思绪的具体内容,更多请存眷资源魔其它相干文章!

标签: php 验证码 php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!