>  기사  >  백엔드 개발  >  PHP利用GD库实现一个简单的验证码

PHP利用GD库实现一个简单的验证码

WBOY
WBOY원래의
2016-07-25 08:42:30748검색
  1. $img=imagecreatetruecolor(100, 40);
  2. $red=imagecolorallocate($img, 255, 0, 0);
  3. $green=imagecolorallocate($img, 0, 255, 0);
  4. $blue=imagecolorallocate($img, 0, 0, 255);
  5. $white=imagecolorallocate($img, 255, 255, 255);
  6. $black=imagecolorallocate($img, 0, 0, 0);
  7. //生成图片
  8. imagefill($img, 0, 0, $black);
  9. //设置验证码
  10. $code="";
  11. for($i=0;$i$code.=rand(0,9);
  12. }
  13. //验证码写到图片中
  14. imagestring($img, 5, 20, 15, $code, $white);
  15. //加点儿干扰
  16. for($i=0;$iimagesetpixel($img, rand(0,100), rand(0,40), $red);
  17. imagesetpixel($img, rand(0,100), rand(0,40), $green);
  18. imagesetpixel($img, rand(0,100), rand(0,40), $blue);
  19. }
  20. //再加点儿干扰
  21. for($i=0;$iimageline($img, rand(0,50), rand(0,20), rand(50,100), rand(20,40), $red);
  22. imageline($img, rand(0,50), rand(0,20), rand(50,100), rand(20,40), $green);
  23. imageline($img, rand(0,50), rand(0,20), rand(50,100), rand(20,40), $blue);
  24. }
  25. header("Content-type:image/png");
  26. imagepng($img);
  27. imagedestroy($img);
  28. ?>
复制代码

验证码, PHP


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.