In fact, there is no need to encapsulate the function, just felt visually good, structure clear point
<?php class captcha{ //Verification Code-string private $codes; //image Length private $img _ length = 150; //picture height private $img _height = 30; //character list to generate random captcha private $charlist = ' 1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM '; //Random code number private $code _num = 4; //Font Size--calculated at initialization private $font _size ; //number of interference lines private $line _ num = 5; //number of interference snowflakes private $sterisk _num = 50; //Verification code--Pictures private $img; // Font file path private $ttf = './instance/font/elephant.ttf '; public function __construct () { //font size is dynamically generated by picture width and height, but not very perfect $this->font_size = ($this->img_height*2/5 > $this->img_ height*4/5 ? $this->img_height*4/5 : $this->img_height*2/5); } public function run () { //creating a picture resource $this->createimage (); //Add Snowflakes $this to the picture Addasterisk (); //add characters to the picture $this->addfont() //add a line to the picture $ This->addline (); //image output to browser $this->outputimg (); } //returns the CAPTCHA string public function getcode () { return $this->codes; } //Create a picture resource private function createimage () { // Create a picture resource $this->img = imagecreatetruecolor ($this- >img_length, $this->img_height) //Create color $color _bg = imagecolorallocate ($this->img, mt_rand (210, 255), mt_Rand (210, 255), mt_rand (210, 255)) //Set picture background color imagefill ($this->img, 0, 0, $color _BG); } //add lines to the picture Private function addline () { //Adds a specified number of lines for ($i = 0; $i < $this->line_num; $i + +) { //create random colors--parameters (picture resources, R, B,G) $color _line = Imagecolorallocate ($this->img, mt_rand (50, 200), mt_rand (50, 200), mt_rand (50, 200)); //add line, Position random--parameters (picture resource, start-X, Start-y, End-X, end-y, color) //Non-adjustable //imageline ($this- >img, mt_rand (0, $this->img_length), mt_rand (0, $this->img_height), mt_rand ( 0, $this->img_length), mt_rand (0, $this->img_height), $color _line); //can adjust the thickness of the line $src _x = mt_rand (0, $this->img_length); $src _y = mt_rand (0, $this->img_ Height); $dest _x = mt_rand (0, $this->img_length); $dest _y = mt_rand (0, $This->img_height); for ($j = 0; $j < 1; $j +) { imageline ($this->img, $src _x+ $j, $src _y+ $j, $dest _x+ $j, $dest _y+ $j, $color _line); } } } // Add snowflakes to the picture private function addasterisk () { //add a specified number of snowflakes for ($i = 0; $i < $this->sterisk_num; $i + +) { //Create random colors--parameters (picture resources, R,b,g) $color _ster = iMagecolorallocate ($this->img, mt_rand (220, 255), mt_rand (220, 255), mt_rand (220, 255)); //add snowflakes, Position random--parameters (picture resource, tilt angle, Lower left-X, lower-left corner-y, Color, string) imagestring ($this- >img,mt_rand (0,360), Mt_rand (0, $this->img_length), Mt_rand (0, $this->img_height), ' * ', $color _ster); } } private function addfont () { for ($i = 0; $i < $this->code_num; $i + +) { //randomly take a character from a character list $code = substr (str_shuffle ($this->charlist), -1); //record to Captcha string $this- >codes .= $code; // Create random colors--parameters (picture resources, r,b,g) $color _font = imagecolorallocate ($this->img, mt_rand (10, 180), mt_rand (10, 180), &NBSP;MT _rand (10, 180)); //Add Snowflakes, Location Random--parameters (picture resource, font size, tilt angle, lower left corner-X, lower left corner-y, font color, font, string) // bottom Left-y, the base height of the font is estimated, because the font size uses points, the length-width pixels of different characters vary greatly imagettftext ($this->img, $this->font_size, mt_rand ( -30, 30), ($this->img_length/$this->code_num) * $i +mt_rand (1, $this->font_size*0.2), $this->img_ Height*0.7+mt_rand (-$this->img_height*0.2, , $this->img_height*0.2), $color _font, $this->ttf, $code); } } //output image to browser private function outputimg () { //Notification Browser is the PNG format header (' Content-type:image/png '); //output in PNG format Imagepng ($this->img); //destroying image resources in memory imagedestroy ($this->img); } public function __set ($key, $value) { } public function __get ($value) { } }
PHP Broken code--Write your own verification code