PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/cmsdemo/Include/GetVerifyImg.php

http://cmsdemo1.googlecode.com/
PHP | 50 lines | 45 code | 0 blank | 5 comment | 4 complexity | ef1e5c2824255e693e73ff6c64044646 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. $length = 4; //定义字符串长度
  3. $strNum = GetVerify($length); //获取一个随机字符串
  4. $_SESSION['verify'] = $strNum; //付值给session
  5. $fontSize = 15; //定义字体大小
  6. $width = 70; //定义图片宽度
  7. $height = 24; //定义图片高度
  8. $im = imagecreate($width,$height); //生成一张指定宽高的图片
  9. $backgroundcolor = imagecolorallocate ($im, 255, 255, 255); //生成背景色
  10. $frameColor = imageColorAllocate($im, 150, 150, 150); //生成边框色
  11. $font = realpath("../Include/arial.ttf"); //提取字体文件,开始写字
  12. for($i = 0; $i < $length; $i++) {
  13. $charY = ($height+9)/2 + rand(-1,1); //定义字符Y坐标
  14. $charX = $i*15+8; //定义字符X坐标
  15. //生成字符颜色
  16. $text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200));
  17. $angle = rand(-20,20); //生成字符角度
  18. //写入字符
  19. imageTTFText($im, $fontSize, $angle, $charX, $charY, $text_color, $font, $strNum[$i]);
  20. }
  21. for($i=0; $i <= 5; $i++) { //循环画背景线
  22. $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  23. $linex = mt_rand(1, $width-1);
  24. $liney = mt_rand(1, $height-1);
  25. imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
  26. }
  27. for($i=0; $i <= 32; $i++) { //循环画背景点,生成麻点效果
  28. $pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  29. imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor);
  30. }
  31. imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor); //画边框
  32. ob_clean();
  33. header('Content-type: image/png');
  34. imagepng($im);
  35. imagedestroy($im);
  36. /**
  37. * 功能:生成随机字符串
  38. */
  39. function GetVerify($length)
  40. {
  41. $strings = Array('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');
  42. $chrNum = "";
  43. $count = count($strings);
  44. for ($i = 1; $i <= $length; $i++) { //循环随机取字符生成字符串
  45. $chrNum .= $strings[rand(0,$count-1)];
  46. }
  47. return $chrNum;
  48. }
  49. exit;
  50. ?>