生成验证码

WBOY
發布: 2016-07-25 08:44:00
原創
890 人瀏覽過
  1. require_once('string.func.php');
  2. function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){
  3. session_start();
  4. /*定义长度和宽度*/
  5. $width=80;
  6. $height=30;
  7. /* 创建画布*/
  8. $image=imagecreatetruecolor($width, $height);
  9. /*本函数用来匹配图形的颜色,供其它绘图函数使用。参数 image 表示图形的 handle。参数 red、green、blue 是色彩三原色,其值从 0 至 255....我在此定义黑色和白色*/
  10. $white=imagecolorallocate($image, 255, 255, 255);
  11. $black=imagecolorallocate($image,0,0,0);
  12. /*本函数将图片的封闭长方形区域着色。参数 x1、y1 及 x2、y2 分别为矩形对角线的坐标。参数 col 表示欲涂上的颜色*/
  13. imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);
  14. /*buildRandomString函数用来生成一个验证码*/
  15. $chars=buildRandomString($type,$length);
  16. /*将验证码给session以便用来判断用户输入是否正确*/
  17. $_SESSION[$sess_name]=$chars;
  18. /*定义字体库*/
  19. $fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');
  20. /*用循环来将验证码一个一个的写入图片中*/
  21. for($i=0;$i{
  22. $size=mt_rand(14,18);
  23. $angle=mt_rand(-15,15);
  24. /*验证码的横坐标与纵坐标*/
  25. $x=5+$i*$size;
  26. $y=mt_rand(20,26);
  27. $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90));
  28. $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
  29. $text=substr($chars,$i,1);
  30. /*本函数将 TTF (TrueType Fonts) 字型文字写入图片*/
  31. imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
  32. }
  33. if($pixel)
  34. {
  35. for($i=0;$i{
  36. /*本函数可在图片上绘出一点。参数 x、y 为欲绘点的坐标,参数 col 表示该点的颜色*/
  37. imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black);
  38. }}
  39. if($line)
  40. {
  41. for($i=0;$i {
  42. $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90));
  43. /*画线段*/
  44. imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);
  45. }
  46. }
  47. /*以gif形式输出*/
  48. header("content-type:image/gif");
  49. /*建立GIF图 并输出到网页*/
  50. imagegif($image);
  51. /*释放与 image 关联的内存*/
  52. imagedestroy($image);
  53. }
复制代码
  1. function buildRandomString($type=1,$length=4){
  2. if($type==1)
  3. {
  4. /*join函数把数组转换为字符串。。join() 函数是 implode() 函数的别名*/
  5. $chars=join("",range(0,9));
  6. }elseif ($type==2) {
  7. /*array_merge函数合并数组*/
  8. $chars=join("",array_merge(range("a","z"),range("A","Z")));
  9. }elseif($type==3)
  10. {
  11. $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
  12. }
  13. if($length>strlen($chars))
  14. {
  15. exit("字符串长度不够");
  16. }
  17. /*打乱字符串*/
  18. $chars=str_shuffle($chars);
  19. return substr($chars,0,$length);
  20. }
  21. ?>
复制代码
验证码


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!