-
- class CCheckCodeFile
- {
- //Number of verification code digits
- private $mCheckCodeNum = 4;
-
- //Generated verification code
- private $mCheckCode = '';
-
- //Verification code Picture
- private $mCheckImage = '';
-
- //Interference pixel
- private $mDisturbColor = '';
-
- //Verification code picture width
- private $mCheckImageWidth = '80';
-
- //Verification code picture Width
- private $mCheckImageHeight = '20';
-
- /**
- *
- * @brief output header
- *
- */
- private function OutFileHeader()
- {
- header ("Content-type: image/png");
- }
-
- /**
- *
- * @brief generates verification code
- *
- */
- private function CreateCheckCode()
- {
- $this->mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));
- return $this->mCheckCode ;
- }
-
- /**
- *
- * @brief generates verification code image
- *
- */
- private function CreateImage()
- {
- $this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);
- imagecolorallocate ( $this->mCheckImage, 200, 200, 200);
- return $this->mCheckImage;
- }
-
- /**
- *
- * @brief Set the interference pixels of the image
- *
- */
- private function SetDisturbColor()
- {
- for ($i=0 ;$i<=128;$i++)
- {
- $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
- imagesetpixel($this ->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
- }
- }
-
- /**
- *
- * @brief Set the size of the verification code image
- *
- * @param $width width
- *
- * @param $height height
- *
- */
- public function SetCheckImageWH($width,$height)
- {
- if($width==''||$height=='')return false;
- $this->mCheckImageWidth = $width;
- $this->mCheckImageHeight = $height;
- return true;
- }
-
- /**
- *
- * @brief Draw the verification codes one by one on the verification code picture
- *
- */
- private function WriteCheckCodeToImage()
- {
- for ($i=0;$i<=$this->mCheckCodeNum;$i++)
- {
- $bg_color = imagecolorallocate ($ this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
- $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
- $y = rand(0,$this->mCheckImageHeight-15);
- imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
- }
- }
-
- /**
- *
- * @brief Output verification code picture
- *
- */
- public function OutCheckImage()
- {
- $this ->OutFileHeader();
- $this ->CreateCheckCode();
- $this ->CreateImage();
- $this ->SetDisturbColor();
- $this ->WriteCheckCodeToImage();
- imagepng($this->mCheckImage);
- imagedestroy($this->mCheckImage);
- }
- }
- $c_check_code_image = new CCheckCodeFile();
- //$c_check_code_image ->SetCheckImageWH(100,50);//Set the size of the verification code image
- $c_check_code_image ->OutCheckImage();
- ?>
Copy code
|