网站地图    收藏   

主页 > php专栏 > php综合实列 >

php标准生成验证码程序 - 综合实例

来源:自学PHP网    时间:2014-12-02 13:09 作者: 阅读:次

[导读] ?phpsession_start();$enablegd=1; 判断图像处理函数是否存在$funcs=array( 39;imagecreatetruecolor 39;, 39;imagecolorallocate 39;, 39;imagefill 39;, 39;imagestring 39;, 39;imag...

php标准生成验证码程序

  1. <?php 
  2. session_start(); 
  3. $enablegd = 1; 
  4. //判断图像处理函数是否存在 
  5. $funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellips教程e','imagepng'); 
  6. foreach($funcs as $func) 
  7. { 
  8.  if(!function_exists($func)) 
  9.  { 
  10.   $enablegd = 0; 
  11.   break; 
  12.  } 
  13. } 
  14.  
  15. ob_clean(); //清理缓冲 
  16.  
  17. if($enablegd) 
  18. { 
  19.  //create captcha 
  20.  $consts = 'cdfgkmnpqrstwxyz23456'; 
  21.  $vowels = 'aek23456789'; 
  22.  for ($x = 0; $x < 6; $x++) 
  23.  { 
  24.   $const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数 
  25.   $vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数 
  26.  } 
  27.  $radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4]; 
  28.  $_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符 
  29.  
  30.  $imageX = strlen($radomstring)*8; //图像的宽 
  31.  $imageY = 20;      //图像的高 
  32.  $im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像 
  33.  
  34.  //creates two variables to store color 
  35.  $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色 
  36.  $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)), 
  37.   imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)), 
  38.   imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)), 
  39.   imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)) 
  40.  ); 
  41.  $foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度 
  42.  $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景 
  43.  $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2 
  44.  
  45.  //与左上角的颜色相同的都会被填充 
  46.  imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254)); 
  47.  //往图像上写入文字 
  48.  imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf', $string[0]); 
  49.  imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf', $string[1]); 
  50.  imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)],'C:WindowsFontsArial.ttf', $string[2]); 
  51.  imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)],'C:WindowsFontsArial.ttf', $string[3]); 
  52.  
  53.  //画边框 
  54.  $border = imagecolorallocate($im, 133, 153, 193); 
  55.  imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border); 
  56.  
  57.  //画一些随机出现的点 
  58.  $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); 
  59.  for ($i=0;$i<80;$i++) 
  60.  { 
  61.   imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol); 
  62.  } 
  63.  //画随机出现的线 
  64.  for ($x=0; $x<9;$x++) 
  65.  { 
  66.   if(mt_rand(0,$x)%2==0) 
  67.   { 
  68.    imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线 
  69.    imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆 
  70.   } 
  71.   else 
  72.   { 
  73.    imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); 
  74.    imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground); 
  75.   } 
  76.  } 
  77.  //output to browser 
  78.  header("content-type:image/pngrn"); 
  79.  imagepng($im); 
  80.  imagedestroy($im); 
  81. } 
  82. else 
  83. { 
  84.  $files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg'); 
  85.  if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/'); 
  86.  
  87.  $checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件 
  88.  $_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名 
  89.  
  90.  header("content-type:image/jpegrn"); 
  91.  include $checkcodefile; 
  92. } 
  93. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论