-
- $authnum='';
- $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E ,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- $list=explode( ",",$ychar);//Split function
- for($i=0;$i<4;$i++){
- $randnum=rand(0,35);
- $authnum.=$list[$randnum ];//Output in the form of an array
Copy code Method 2, defined as a private function.
- private function createCheckCode()
- {
- for(i=0;icodeNum;i++)
- {
- number = rand(0,2);
- switch( number)
- {
- case 0: rand_number = rand(48,57); break;//Number
- case 1: rand_number = rand(65,90);break;//Capital letters
- case 2: rand_number = rand(97,122 );break;//Lowercase letters
- }
- $asc = sprintf("%c",rand_number);
- $asc_number = asc_number.asc;
- }
- return asc_number;
- }
Copy code
Method 3 , use random seeds to generate PHP verification codes.
-
- srand(microtime()*100000);//Equivalent to timer
- $string="abcdefghigklmnopqrstuvwxyz123456789";
- for($i=0;$i<4;$i++)
- {
- $new_number.=$string[rand(0,strlen($string)-1)];//Immediately generate an array
- }
Copy code Method 4,
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1,15));//Convert decimal to hexadecimal
- }
Copy the codeThen let’s get to the focus of this article:
PHP GD library: (Provides a series of IPIs for image processing functions to generate image processing images)
Enable the GD library in php: In the php.ini configuration file, remove the ";" in ";extension=php_gd2.dll";
Introduction to some GD library functions:
1.imagecreatetruecolor(int x_size,int Y_size) Create a new true color image
2.imagecolorallocate(resource image,int red,int green,int blue) assigns color to an image, three primary colors
3.imagestring(resource,font,int x,int y,content,color) drawing function 4.header("Content-type:image/jpeg") The header of the output function php is an action that defines the header. php5 supports 3 of them. Type: 1, Content-type: xxxx/yyyy 2, Location: xxxx:yyyy/zzzz 3, Status: nnn xxxxxx xxxx/yyyy indicates the type of content file such as: image/gif image/jpeg image/png imagejpeg(), imagegif (),imagepang() 5.iamgeline(resource image,int x1,int y1,int x2,int y2,int color); Line drawing function, (int x, int y) starting coordinates
6.imagesetpixel(resource image,int x,int y,int color) drawing point function
7.imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) with font writing function 8.iconv("gb2312","utf-8","string ");
//First, convert the text into UTF-8 format. How to insert the PHP verification code into Chinese. Here you can refer to the method of generating PHP Chinese verification code.
Example 1, randomly generate numbers and letter codes:
- //che.php
- session_start();
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1, 15));
- }
- $_SESSION['check_num']=$rand;
- $image=imagecreatetruecolor(50,30);
- $bg=imagecolorallocate($im,0,0,0);//First time When using the palette, the background color
- $te=imagecolorallocate($im,255,255,255);
- imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
- ob_clean();//The image "http://localhost/**.php" appears in the PHP webpage because it needs to generate a verification code because it has errors and cannot be displayed
- header("Content-type:image/jpeg") ; imagejpeg($image);
- ?>
Copy the code
Example 2, draw interference line code for the picture:
-
- for($i=0;$i<8;$i++)//Draw multiple lines
- {
- $cg=imagecolorallocate($im,rand(0,255),rand (0,255),rand(0,255));//Generate random colors
- imageline($im,rand(10,40),0,rand(10,40),20,$cg);
- }
Copy Code Example 3, code to draw interference points on pictures:
- for($i=0;$i<80;$i++)//Draw multiple points
- {
- imagesetpixel($im,rand(0,40),rand( 0,20),$cg);
- }
Copy codeExample 4, write text into image code:
$str=array('i','i','pro','pro');//Storage displayed Chinese characters - for($i= 0;$i<4;$i++)
- {
- $sss.=$str[rand(0,3)];//Display Chinese characters randomly
- }
//$str= iconv("gb2312","utf-8",$str); //Chinese character encoding conversion, mine doesn’t seem to need it
- imagettftext($im,10,0,rand(5,60),rand(5,60) ,$te,"simhei.ttf",$sss);//
-
Copy code
0: the inclination of the font, "simhei.ttf": the font style, usually placed at the root Under the directory;
This completes the entire production process of PHP image verification code. The script editor hopes that this article will be helpful to use the gd library to generate PHP verification code.
|