Vcode.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. file: vcode.class.php
  4. 验证码类,类名Vcode
  5. */
  6. class Vcode {
  7. private $width; //验证码图片的宽度
  8. private $height; //验证码图片的高度
  9. private $codeNum; //验证码字符的个数
  10. private $disturbColorNum; //干扰元素数量
  11. private $checkCode; //验证码字符
  12. private $image; //验证码资源
  13. /**
  14. * 构造方法用来实例化验证码对象,并为一些成员属性初使化
  15. * @param int $width 设置验证码图片的宽度,默认宽度值为80像素
  16. * @param int $height 设置验证码图片的高度,默认高度值为20像素
  17. * @param int $codeNum 设置验证码中字母和数字的个数,默认个数为4个
  18. */
  19. function __construct($width=80, $height=20, $codeNum=4) {
  20. $this->width = $width;
  21. $this->height = $height;
  22. $this->codeNum = $codeNum;
  23. $number = floor($height*$width/15);
  24. if($number > 240-$codeNum)
  25. $this->disturbColorNum = 240-$codeNum;
  26. else
  27. $this->disturbColorNum = $number;
  28. $this->checkCode = array();
  29. $this->checkCode = $this->createCheckCode();
  30. }
  31. /**
  32. * 用于输出验证码图片,也向服务器的SESSION中保存了验证码,使用echo 输出对象即可
  33. */
  34. function __toString(){
  35. /* 加到session中, 存储下标为code */
  36. $_SESSION["code"] = strtoupper($this->checkCode);
  37. $this->outImg();
  38. return '';
  39. }
  40. /* 内部使用的私有方法,用于输出图像 */
  41. private function outImg(){
  42. $this->getCreateImage();
  43. $this->setDisturbColor();
  44. $this->outputText();
  45. $this->outputImage();
  46. }
  47. /* 内部使用的私有方法,用来创建图像资源,并初使化背影 */
  48. private function getCreateImage(){
  49. $this->image = imagecreatetruecolor($this->width,$this->height);
  50. $backColor = imagecolorallocate($this->image, rand(225,255),rand(225,255),rand(225,255));
  51. @imagefill($this->image, 0, 0, $backColor);
  52. $border = imageColorAllocate($this->image, 0, 0, 0);
  53. imageRectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
  54. }
  55. /* 内部使用的私有方法,随机生成用户指定个数的字符串,去掉了容易混淆的字符oOLlz和数字012 */
  56. private function createCheckCode(){
  57. $code="3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY";
  58. for($i=0; $i<$this->codeNum; $i++) {
  59. $char = $code{rand(0,strlen($code)-1)};
  60. $ascii .= $char;
  61. }
  62. return $ascii;
  63. }
  64. /* 内部使用的私有方法,设置干扰像素,向图像中输出不同颜色的点 */
  65. private function setDisturbColor() {
  66. for($i=0; $i <= $this->disturbColorNum; $i++) {
  67. $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
  68. imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  69. }
  70. for($i=0; $i<5; $i++){
  71. $color=imagecolorallocate($this->image,rand(100,255),rand(100,255),rand(100,255));
  72. imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,200),55,44,$color);
  73. }
  74. }
  75. /* 内部使用的私有方法,随机颜色、随机摆放、随机字符串向图像中输出 */
  76. private function outputText() {
  77. for ($i=0; $i<=$this->codeNum; $i++) {
  78. $fontcolor = imagecolorallocate($this->image, rand(0,50), rand(0,50), rand(0,50));
  79. $fontSize = 5;//rand(3,5);
  80. $x = floor($this->width/$this->codeNum)*$i+3;
  81. $y = rand(0,$this->height-imagefontheight($fontSize));
  82. imagechar($this->image, $fontSize, $x, $y, $this->checkCode{$i}, $fontcolor);
  83. }
  84. }
  85. /* 内部使用的私有方法,自动检测GD支持的图像类型,并输出图像 */
  86. private function outputImage(){
  87. if(imagetypes() & IMG_GIF){
  88. header("Content-type: image/gif");
  89. imagegif($this->image);
  90. }elseif(imagetypes() & IMG_JPG){
  91. header("Content-type: image/jpeg");
  92. imagejpeg($this->image, "", 0.5);
  93. }elseif(imagetypes() & IMG_PNG){
  94. header("Content-type: image/png");
  95. imagepng($this->image);
  96. }elseif(imagetypes() & IMG_WBMP){
  97. header("Content-type: image/vnd.wap.wbmp");
  98. imagewbmp($this->image);
  99. }else{
  100. die("PHP不支持图像创建!");
  101. }
  102. }
  103. /* 析构方法,在对象结束之前自动销毁图像资源释放内存 */
  104. function __destruct(){
  105. imagedestroy($this->image);
  106. }
  107. }