汉字验证码

verify.php

<?php 
      session_start();

      $image=imagecreatetruecolor(150, 35);
      $bgcolor=imagecolorallocate($image, 255, 255, 255);
      imagefill($image, 0, 0, $bgcolor);
      
     $fontface='a.ttf';//字体一定要命名为英文,本地或者服务器上一定要有该字体文件,否则图片无法显示</span>
	  
      $str = "艺 的 一 是 在 了 不 和 有 大 这 主 中 人 上 为 们 地 个 用 工 时 要 动 国 产 以 我 到 他 会 作 来 分 生 对 于 学 下 级 就 年 阶 义 发 成 部 民 可 出 能 方 进 同 行 面 说 种 过 命 度 革 而 多 子 后 自 社 加 小 机 也 经 力 线 本 电 高 量 长 党 得 实 家 定 深 法 表 着 水 理 化 争 现 所 二 起 政 三 好 十"; 
      $strdb = explode(" ", $str);
     
     $captch_code='';
     for($i=0;$i<1;$i++)//写汉字       
     { 
         $fontcolor=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));//字体颜色
        
           $index = rand(0, (count($strdb)-1)); //计算数组中的单元数目或对象中的属性个数
            $cn = $strdb[$index];
            $captch_code.=$cn;

         imagettftext($image, mt_rand(20,24), mt_rand(-20,60),(40*$i+20), mt_rand(30,35),  $fontcolor, $fontface, $cn);
         //imagettftext(image, size, angle, x, y, color, fontfile, text)
         //用 TrueType 字体向图像写入文本
         //angle 更高数值表示逆时针旋转
       } 
      

       $_SESSION['authcode']=$captch_code;

      for ($i=0; $i < 200; $i++) //干扰点
      { 
      	$pointcolor=imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
      	imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
      }
      
       for ($i=0; $i < 3; $i++) //干扰线
      { 
      	$linecolor=imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));
      	imageline($image, rand(1,199), rand(1,59),rand(1,199), rand(1,59), $linecolor);
      }

      header('content-type: image/png');
      imagepng($image);

      imagedestroy($image);
      
?>

 运行结果:


没有字体文件的运行图片:


补充一下:

如果想汉字,中文,字母都有改一下上面的字符串,例如$str = "艺 的 一 是 在 1 2 3 4 5 6 7 8 9 0 A B C D E F"; 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值