PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/captcha.php

https://code.google.com/p/ezrpg/
PHP | 82 lines | 58 code | 21 blank | 3 comment | 1 complexity | aae53f6e1cdd337e731cad125e0bc27d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. define('IN_EZRPG', true);
  3. require_once('init.php');
  4. //$code_length = rand(5,6);
  5. $code_length = 4;
  6. $rand_start = mt_rand(0, 250);
  7. $font = 'static/fonts/Capture_it.ttf';
  8. $fontSize = 30;
  9. $padding = 10;
  10. $l1 = strtoupper(createKey(1, 1));
  11. $l2 = strtoupper(createKey(1, 1));
  12. $l3 = strtoupper(createKey(1, 1));
  13. $l4 = strtoupper(createKey(1, 1));
  14. $verify_string = $l1 . ' ' . $l2 . ' ' . $l3 . ' ' . $l4;
  15. $real_string = $l1 . $l2 . $l3 . $l4;
  16. $verify_code = sha1(strtoupper($real_string) . SECRET_KEY);
  17. $_SESSION['verify_code'] = $verify_code;
  18. function makeRBGColor($color, $image)
  19. {
  20. $color = str_replace("#", "", $color);
  21. $red = hexdec(substr($color, 0, 2));
  22. $green = hexdec(substr($color, 2, 2));
  23. $blue = hexdec(substr($color, 4, 2));
  24. $out = ImageColorAllocate($image, $red, $green, $blue);
  25. return $out;
  26. }
  27. $wordBox = imageftbbox($fontSize, 0, $font, $verify_string);
  28. $wordBoxWidth = $wordBox[2];
  29. $wordBoxHeight = $wordBox[1] + abs($wordBox[7]);
  30. $containerWidth = $wordBoxWidth + ($padding * 2);
  31. $containerHeight = $wordBoxHeight + ($padding * 2);
  32. $textX = $padding;
  33. $textY = $containerHeight - $padding;
  34. $captchaImage = imagecreate($containerWidth, $containerHeight);
  35. $red = randColor();
  36. $green = randColor();
  37. $blue = randColor();
  38. $backgroundColor = ImageColorAllocate($captchaImage, $red, $green, $blue);
  39. $rred = 255-$red;
  40. $rgreen = 255-$green;
  41. $rblue = 255-$blue;
  42. $textColor = ImageColorAllocate($captchaImage, $rred, $rgreen, $rblue);
  43. imagefttext($captchaImage, $fontSize, 0, $textX, $textY, $textColor, $font, $verify_string);
  44. $angle = mt_rand(-3, 3);
  45. $captchaImage = imagerotate($captchaImage, $angle, $backgroundColor);
  46. $line = ImageColorAllocate($captchaImage, $rred, $rgreen, $rblue);
  47. for($i = 0; $i < 10; $i++)
  48. {
  49. $xStart = mt_rand(0, $containerWidth);
  50. $yStart = mt_rand(0, $containerHeight);
  51. $xEnd = mt_rand(0, $containerWidth);
  52. $yEnd = mt_rand(0, $containerHeight);
  53. imageline($captchaImage, $xStart, $yStart, $xEnd, $yEnd, $line);
  54. }
  55. imagefilter($captchaImage, IMG_FILTER_CONTRAST, 1);
  56. //imagefilter($captchaImage, IMG_FILTER_BRIGHTNESS, 10);
  57. //imagefilter($captchaImage, IMG_FILTER_EDGEDETECT);
  58. imagefilter($captchaImage, IMG_FILTER_GAUSSIAN_BLUR);
  59. header('Content-Type:image/png');
  60. ImagePNG($captchaImage);
  61. ?>