captcha.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'VestaSession.class.php';
  3. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'Config.class.php';
  4. define('V_ROOT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vesta' . DIRECTORY_SEPARATOR);
  5. class Captcha
  6. {
  7. protected $width = 200;
  8. protected $image = null;
  9. protected $color1 = null;
  10. protected $color2 = null;
  11. protected $color3 = null;
  12. protected $keyword = '';
  13. public $key_len = 7;
  14. protected $chars = 'qw1e2r3ty5u678p97as9d3o87f6gh3j2k73z5x7c8v3b75n77812356789';
  15. public function __construct()
  16. {
  17. VestaSession::start();
  18. //var_dump(Config::get('session_dirname'));die();
  19. $this->image = imagecreatetruecolor($this->width, 50);
  20. $this->color1 = imagecolorallocate($this->image, 57, 58, 52);
  21. $this->color2 = imagecolorallocate($this->image, 45, 44, 40);
  22. $this->color3 = imagecolorallocate($this->image, 255, 255, 255);
  23. imagefilledrectangle($this->image, 0, 0, 249, 249, $this->color1);
  24. }
  25. public function generateImage($offset = 0)
  26. {
  27. $values = array(
  28. $offset, 15,
  29. $offset, 40,
  30. $offset + 14, 32,
  31. $offset + 14, 8,
  32. $offset, 15,
  33. $offset, 15
  34. );
  35. imagefilledpolygon($this->image, $values, 6, $this->color2);
  36. }
  37. public function draw()
  38. {
  39. $this->generateKeyword();
  40. for ($i = 0; $i < strlen($this->keyword) -1; $i++) {
  41. $this->generateImage($i * 15);
  42. }
  43. $font_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'css'.DIRECTORY_SEPARATOR.'arialbd.ttf';
  44. imagefttext($this->image, 17, 0, 2, 31, $this->color3, $font_file, $this->keyword);
  45. $this->slice();
  46. }
  47. public function slice()
  48. {
  49. $width = 15;
  50. $height = 50;
  51. $dest = imagecreatetruecolor(15 * $this->key_len + 2 * $this->key_len + 8, $height);
  52. imagefilledrectangle($dest, 0, 0, 249, 249, $this->color1);
  53. for ($i = 0; $i < $this->key_len; $i++) {
  54. $dest_x = $i == 0 ? $i * 15 : $i * 15 + $i * 4;
  55. imagecopy($dest, $this->image, $dest_x, 0, $i * 15, 0, $width, $height);
  56. }
  57. header('Content-type: image/jpeg');
  58. imagepng($dest);
  59. }
  60. /**
  61. *
  62. */
  63. protected function generateKeyword()
  64. {
  65. $this->keyword = '';
  66. for ($i = 0; $i < $this->key_len; $i++) {
  67. $this->keyword .= $this->chars[rand(0, strlen($this->chars)-1)];
  68. }
  69. $_SESSION['captcha_key'] = $this->keyword;
  70. return $this->keyword;
  71. }
  72. }
  73. $c = new Captcha();
  74. $c->draw();
  75. ?>