php生成图片验证码


  1. public function valiCodeImage($valicode_name=valicode’)
  2.         {
  3.                 $str = self::_random(5); //随机生成的字符串
  4.                 $width = 60; //验证码图片的宽度
  5.                 $height = 25; //验证码图片的高度
  6.                 @header("Content-Type:image/png");
  7.                 $im=imagecreate($width,$height);
  8.                 //背景色
  9.                 $back=imagecolorallocate($im,0xFF,0xFF,0xFF);
  10.                 //模糊点颜色
  11.                 $pix=imagecolorallocate($im,187,230,245);
  12.                 //字体色
  13.                 $font=imagecolorallocate($im,40,160,230);
  14.                 //绘模糊作用的点
  15.                 mt_srand();
  16.                 for($i=0;$i<1000;$i++)
  17.                 {
  18.                         imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
  19.                 }
  20.                 imagestring($im, 5, 7, 5,$str, $font);
  21.                 imagerectangle($im,0,0,$width-1,$height-1,$font);
  22.                 imagepng($im);
  23.                 imagedestroy($im);
  24.  
  25.                 if( empty($valicode_name) ){
  26.                         $valicode_name = valicode’;
  27.                 }
  28.                 $_SESSION[$valicode_name] = $str;
  29.                 //echo $str;
  30.         }
  31. private function _random($len)  {
  32.                 $srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  33.                 mt_srand();
  34.                 $strs="";
  35.                 for($i=0;$i<$len;$i++){
  36.                         $strs.=$srcstr[mt_rand(0,61)];
  37.                 }
  38.                 return $strs;
  39.         }
  1. No comments yet.
(will not be published)

  1. No trackbacks yet.