/phpsso_server/phpcms/libs/classes/checkcode.class.php
https://github.com/hxzyzz/ddc · PHP · 143 lines · 81 code · 24 blank · 38 comment · 11 complexity · 464d23134b3ec8927fb34386b65921ac MD5 · raw file
- <?php
- /**
- * Éú³ÉÑéÖ¤Âë
- * @author chenzhouyu
- * ÀàÓ÷¨
- * $checkcode = new checkcode();
- * $checkcode->doimage();
- * //È¡µÃÑéÖ¤
- * $_SESSION['code']=$checkcode->get_code();
- */
- class checkcode {
- //ÑéÖ¤ÂëµÄ¿í¶È
- public $width=130;
-
- //ÑéÖ¤ÂëµÄ¸ß
- public $height=50;
-
- //ÉèÖÃ×ÖÌåµÄµØÖ·
- public $font;
-
- //ÉèÖÃ×ÖÌåÉ«
- public $font_color;
-
- //ÉèÖÃËæ»úÉú³ÉÒò×Ó
- public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';
-
- //ÉèÖñ³¾°É«
- public $background = '#EDF7FF';
-
- //Éú³ÉÑéÖ¤Âë×Ö·ûÊý
- public $code_len = 4;
-
- //×ÖÌå´óС
- public $font_size = 20;
-
- //ÑéÖ¤Âë
- private $code;
-
- //ͼƬÄÚ´æ
- private $img;
-
- //ÎÄ×ÖXÖῪʼµÄµØ·½
- private $x_start;
-
- function __construct() {
- $this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'elephant.ttf';
- }
-
- /**
- * Éú³ÉËæ»úÑéÖ¤Âë¡£
- */
- protected function creat_code() {
- $code = '';
- $charset_len = strlen($this->charset)-1;
- for ($i=0; $i<$this->code_len; $i++) {
- $code .= $this->charset[rand(1, $charset_len)];
- }
- $this->code = $code;
- }
-
- /**
- * »ñÈ¡ÑéÖ¤Âë
- */
- public function get_code() {
- return strtolower($this->code);
- }
-
- /**
- * Éú³ÉͼƬ
- */
- public function doimage() {
- $this->creat_code();
- $this->img = imagecreatetruecolor($this->width, $this->height);
- if (!$this->font_color) {
- $this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));
- } else {
- $this->font_color = imagecolorallocate($this->img, hexdec(substr($this->font_color, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));
- }
- //ÉèÖñ³¾°É«
- $background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));
- //»Ò»¸ö¹ñÐΣ¬ÉèÖñ³¾°ÑÕÉ«¡£
- imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);
- $this->creat_font();
- $this->creat_line();
- $this->output();
- }
-
- /**
- * Éú³ÉÎÄ×Ö
- */
- private function creat_font() {
- $x = $this->width/$this->code_len;
- for ($i=0; $i<$this->code_len; $i++) {
- imagettftext($this->img, $this->font_size, rand(-30,30), $x*$i+rand(0,5), $this->height/1.4, $this->font_color, $this->font, $this->code[$i]);
- if($i==0)$this->x_start=$x*$i+5;
- }
- }
-
- /**
- * »Ïß
- */
- private function creat_line() {
- imagesetthickness($this->img, 3);
- $xpos = ($this->font_size * 2) + rand(-5, 5);
- $width = $this->width / 2.66 + rand(3, 10);
- $height = $this->font_size * 2.14;
-
- if ( rand(0,100) % 2 == 0 ) {
- $start = rand(0,66);
- $ypos = $this->height / 2 - rand(10, 30);
- $xpos += rand(5, 15);
- } else {
- $start = rand(180, 246);
- $ypos = $this->height / 2 + rand(10, 30);
- }
-
- $end = $start + rand(75, 110);
-
- imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color);
-
- $color = $colors[rand(0, sizeof($colors) - 1)];
-
- if ( rand(1,75) % 2 == 0 ) {
- $start = rand(45, 111);
- $ypos = $this->height / 2 - rand(10, 30);
- $xpos += rand(5, 15);
- } else {
- $start = rand(200, 250);
- $ypos = $this->height / 2 + rand(10, 30);
- }
-
- $end = $start + rand(75, 100);
-
- imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);
- }
-
- //Êä³öͼƬ
- private function output() {
- header("content-type:image/png\r\n");
- imagepng($this->img);
- imagedestroy($this->img);
- }
- }