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