建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > PHP学习 > php怎么生成中文验证码

php怎么生成中文验证码

时间:2015-02-15 10:16:57  来源:免费模板网 作者:风雪 阅读次数 tags

看到别人的网站有时候是中文验证码,他是怎么实现的呢?原理是什么,请看下文

<?php
 
 
    Header("Content-type: image/PNG");
    $str="这里放入你要显示的中文验证码,出现的验证码会在这里随机出现";
    $imgWidth = 500;//图片宽度
    $imgHeight = 40; //图片高度
    $authimg = imagecreate($imgWidth,$imgHeight);
    $bgColor = ImageColorAllocate($authimg,255,255,255);
    $fontfile = "msyh.ttf";//这里是字体,这个掉了是生成不成功的
    $white=imagecolorallocate($authimg,234,185,95);
    //干扰线
    imagearc($authimg, 150, 8, 20, 20, 75, 170, $white);
    imagearc($authimg, 180, 7,50, 30, 75, 175, $white);
    imageline($authimg,20,20,180,30,$white);
    imageline($authimg,20,18,170,50,$white);
    imageline($authimg,25,50,80,50,$white);
 
    $noise_num = 800;
    $line_num = 20;
    imagecolorallocate($authimg,0xff,0xff,0xff);
    $rectangle_color=imagecolorallocate($authimg,0xAA,0xAA,0xAA);
    $noise_color=imagecolorallocate($authimg,0x00,0x00,0x00);
    $font_color=imagecolorallocate($authimg,0x00,0x00,0x00);
    $line_color=imagecolorallocate($authimg,0x00,0x00,0x00);
 
    //干扰点点点
    for($i=0;$i<$noise_num;$i++){
        imagesetpixel($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$noise_color);
    }
    //干扰线
    for($i=0;$i<$line_num;$i++){
        imageline($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$line_color);
    }
    //随机
    $randnum=rand(0,strlen($str)-4);
    if($randnum%2)$randnum+=1;
    $str = substr($str,$randnum,8);
 
    $str = iconv("UTF-8","UTF-8",$str);
    imagettftext($authimg, 9, 0, 0, 14, $font_color, $fontfile, $str);
    imagepng($authimg);
    imagedestroy($authimg);
 
?>

本文地址:https://www.freemoban.com/php/2015/0215/575.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!