First step: Create a canvas
Step two: Create a color
Step three: Prepare the characters
Fourth step: Write characters on the canvas
Fifth step: Draw the interference line (point)
Sixth step: Output format
Seventh Step: Output picture
Eighth Step: Destroying resources
<?php verify () function verify ($width = 100 , $height = 40 , $num = 5 , $type = 3 ) { 1 Create canvas $image = imagecreatetruecolor ($width , $height); //2 Create a color //because it's always used behind, so I wrote two functions, Lightcolor (light color). , Deepcolor (dark color) //3 Create character // Here is the type of your choice,1 is pure digital,2 is a pure small letter, 3 is a mixed number of uppercase and lowercase letters switch ($type) { case 1: //defining strings $str = "0123456789"; //scrambled String $strNew = str_shuffle ($STR); //Interception of $num $string = substr ($strNew , 0 , $num); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BREAK;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;2: //Defining characters $arr = range (' A ' , ' z '); //scrambled string array shuffle ($arr); Interception $tmp = array_slice ($arr , 0 , $num); //string $string = join (' , $tmp); break; case 3: $str = " 23456789abcdefghjklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ "; $string = substr (str_shuffle ($STR) , 0 , $num); break; } //fill the background with light color //the background color is too deep, the verification code will not be clear imagefilledrectangle ($image , 0 , 0 , $width , $height , lightcolor ($image); 4 write character for on canvas ($i = 0; $i < $num; $i + +) { / /Because we set the output 5 characters, so $i is less than $num //floor is rounding, $width / $num Divide the width of the $num block, * $i is a character to occupy a piece of land, so as not to be piled up in a piece $x = floor ($width / $num) * $i; $y = mt_rand (10 , $height - 20); imagechar ($image , 5 , $x , $y , $string [$i] , deepcolor ($image )); } //5 draw interference lines, points //Interference Line for ($i = 0; $i < $num; $i + +) { imagearc ($image , mt_rand (10 , $width) , mt_ Rand (10&NBSP;,&NBSP; $height) , mt_rand (10 , $width) , mt_rand (10 , $height) mt_rand (0 , 10) , mt_rand (0 , 270) , deepcolor ($image)); } //Interference points for ( $i = 0; $i <50; $i + +) { imagesetpixel ($image , mt_rand (0 , $width) , mt_rand (0 , $ Height) , deepcolor ($image)); } ob_clean ();//Erase Buffer //6 tells the browser output format: Png header ("content-type: Image/png "); //7 output picture imagepng ($ Image); //8 destroy imagedestroy ($image) ; return $string; //echo $string;} Set the depth color function lightcolor ($image) { return imagecolorallocate ($ Image , mt_rand (120 , 255) , mt_rand (120 , 255) , mt_rand ( 120 , 255)); }function deepcolor ($image) { return Imagecolorallocate ($image , mt_rand (0 , 120) , mt_rand (0 , 120) mt_rand (0 , 120)); } ?>
Special Note:
Ob_clean ();//Erase Buffer
This was not added before, and the result tells me "cannot be displayed because of errors", such as:
Fortunately, Baidu found the answer: 50845075. Plus, it's easy to solve the problem, and now the effect is this:
All right.
PHP Implementation Verification Code