资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用PHP怎么实现一个汉字验证码功能-创新互联

今天就跟大家聊聊有关使用PHP怎么实现一个汉字验证码功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联建站专注于企业全网整合营销推广、网站重做改版、延长网站定制设计、自适应品牌网站建设、H5开发商城网站制作、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为延长等各大城市提供网站开发制作服务。

创建背景画布


$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怎么实现一个汉字验证码功能-创新互联
文章起源:http://cdkjz.cn/article/dodejp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220