PHP에서 이미지 인증코드를 무작위로 생성하는 방법에 대한 자세한 설명

WBOY
풀어 주다: 2016-07-25 08:52:44
원래의
922명이 탐색했습니다.
  1. session_start();
  2. //随机生成验证码的字符串
  3. function random($len) {
  4. $srcstr="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
  5. mt_srand();
  6. $strs="";
  7. for($i=0;$i<$len;$i ) {
  8. $strs.=$srcstr[mt_rand(0,33)];
  9. }
  10. return ($strs);
  11. }
  12. $str=random(5); //随机生成的字符串
  13. $width=60; //验证码图片的宽度
  14. $height=25; //验证码图片的高度
  15. //Date in the past
  16. header("Expires:Mon,26 Jul 1997 05:00:00 GMT");
  17. //always modified
  18. header("Last-Modified:".gmdate("D,d M Y H:i:s")."GMT");
  19. //HTTP/1.1
  20. header("Cache-Control:no-store,no-cache,must-revalidate");
  21. header("Cache-Control:post-check=0,pre-check=0",false);
  22. //HTTP/1.0
  23. header("Pragma:no-cache");
  24. header("Content-Type:image/png");
  25. $im=imagecreate($width,$height);
  26. $back=imagecolorallocate($im,0xFF,0xFF,0xFF); //背景色
  27. $pix=imagecolorallocate($im,187,190,247); //模糊点颜色
  28. $font=imagecolorallocate($im,41,163,238); //字体色
  29. //绘制1000个模糊作用的点
  30. mt_srand();
  31. for($i=0;$i<1000;$i ) {
  32. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
  33. }
  34. imagestring($im,5,7,5,$str,$font);//绘制随机生成的字符串
  35. imagerectangle($im,0,0,$width-1,$height-1,$font);//在验证码图像周围绘制1px的边框
  36. imagepng($im);//建立一张PNG格式图形
  37. imagedestroy($im);//将图片handle解构,释于内存空间
  38. $_SESSION["auth_code"]=$str;
  39. ?>
复制代码

例2,php随机验证码 php生成简单的验证码

  1. $image_width=140;
  2. $image_height=50;
  3. srand(microtime()*10000);
  4. for($i=0;$i<4;$i ){
  5. $number.=dechex(rand(0,15));
  6. }
  7. $check=$number;
  8. $im=imagecreate($image_width,$image_height);
  9. imagecolorallocate($im,255,255,255);
  10. for($i=0;$i $font=rand(40,80);
  11. $x=rand(1,8) $image_width*$i/4;
  12. $y=rand(1,$image_height/4);
  13. $color=imagecolorallocate($im,rand(0,100),rand(0,150),rand(0,200));
  14. imagestring($im,$font,$x,$y,$check[$i],$color);
  15. }
  16. imagepng($im);
  17. imagedestroy($im);
  18. ?>
复制代码


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!