- 论坛徽章:
- 0
|
php简单验证码
Php代码- 1.<!--
- 2.*@copyright(c)2011
- 3.*@author:Ernest
- 4.*@time:2011
- 5.*@version:new
- 6.-->
- 7.
- 8.<?php
- 9.
- 10.class VerifyImg {
- 11. public $fontSize = 15; //定义字体大小
- 12. public $length = 4; //定义字符串长度
- 13. public $width = 70; //定义图片宽度
- 14. public $height = 30; //定义图片高度
- 15. public $im = null; //生成一张指定宽高的图片
- 16. public $font = 'C:/Windows/Fonts/Arial.TTF';
- 17. public $strNum = "";
- 18. public function Build() {
- 19. $strings = Array ('1', '2','3', '4', '5', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' );
- 20.// $strNum = "";
- 21. $count = count ( $strings );
- 22. for($i = 1; $i <= $this->length; $i ++) { //循环随机取字符生成字符串
- 23. $strNum .= $strings [rand ( 0, $count - 1 )];
- 24. }
- 25. session_start ();
- 26. $_SESSION ["verifycode"] = $strNum;
- 27. $this->im = imagecreate ( $this->width, $this->height );
- 28. $backgroundcolor = imagecolorallocate ( $this->im, 255, 255, 255 ); //生成背景色
- 29. $frameColor = imageColorAllocate ( $this->im, 0, 255, 0 );
- 30. for($i = 0; $i < $this->length; $i ++) {
- 31. $charY = ($this->height + 9) / 2 + rand ( - 1, 1 ); //定义字符Y坐标
- 32. $charX = $i * 15 + 8; //定义字符X坐标
- 33. // $text_color = imagecolorallocate($this->im, 255, 0, 0); //生成字符颜色
- 34. $text_color = imagecolorallocate ( $this->im, mt_rand ( 50, 255 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );
- 35. $angle = rand ( - 20, 20 ); //生成字符角度
- 36. //写入字符
- 37. imagettftext ( $this->im, $this->fontSize, $angle, $charX, $charY, $text_color, $this->font, $strNum [$i] );
- 38. }
- 39. for($i = 0; $i <= 5; $i ++) { //循环画背景线
- 40. $linecolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
- 41. $linex = mt_rand ( 1, $this->width - 1 );
- 42. $liney = mt_rand ( 1, $this->height - 1 );
- 43. imageline ( $this->im, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );
- 44. }
- 45. for($i = 0; $i <= 32; $i ++) { //循环画背景点,生成麻点效果
- 46. $pointcolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
- 47. imagesetpixel ( $this->im, mt_rand ( 1, $this->width - 1 ), mt_rand ( 1, $this->height - 1 ), $pointcolor );
- 48. }
- 49. imagerectangle ( $this->im, 0, 0, $this->width - 1, $this->height - 1, $frameColor ); //画边框
- 50. ob_clean ();
- 51. header ( 'Content-type:image/png' );
- 52. imagepng ( $this->im );
- 53. imagedestroy ( $this->im );
- 54. }
- 55.}
- 56.$img = new VerifyImg ();
- 57.$img->Build ();
- 58.?>
复制代码 |
|