Home  >  Article  >  Backend Development  >  Example of making PHP image verification code

Example of making PHP image verification code

WBOY
WBOYOriginal
2016-07-25 08:55:121014browse
  1. $authnum='';
  2. $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";
  3. $list=explode( ",",$ychar);//Split function
  4. for($i=0;$i<4;$i++){
  5. $randnum=rand(0,35);
  6. $authnum.=$list[$randnum ];//Output in the form of an array
Copy code

Method 2, defined as a private function.

  1. private function createCheckCode()
  2. {
  3. for(i=0;icodeNum;i++)
  4. {
  5. number = rand(0,2);
  6. switch( number)
  7. {
  8. case 0: rand_number = rand(48,57); break;//Number
  9. case 1: rand_number = rand(65,90);break;//Capital letters
  10. case 2: rand_number = rand(97,122 );break;//Lowercase letters
  11. }
  12. $asc = sprintf("%c",rand_number);
  13. $asc_number = asc_number.asc;
  14. }
  15. return asc_number;
  16. }
Copy code

Method 3 , use random seeds to generate PHP verification codes.

  1. srand(microtime()*100000);//Equivalent to timer
  2. $string="abcdefghigklmnopqrstuvwxyz123456789";
  3. for($i=0;$i<4;$i++)
  4. {
  5. $new_number.=$string[rand(0,strlen($string)-1)];//Immediately generate an array
  6. }
Copy code

Method 4,

  1. for($i=0;$i<4;$i++)
  2. {
  3. $rand.=dechex(rand(1,15));//Convert decimal to hexadecimal
  4. }
Copy the code

Then 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:

  1. //che.php
  2. session_start();
  3. for($i=0;$i<4;$i++)
  4. {
  5. $rand.=dechex(rand(1, 15));
  6. }
  7. $_SESSION['check_num']=$rand;
  8. $image=imagecreatetruecolor(50,30);
  9. $bg=imagecolorallocate($im,0,0,0);//First time When using the palette, the background color
  10. $te=imagecolorallocate($im,255,255,255);
  11. imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
  12. 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
  13. header("Content-type:image/jpeg") ; imagejpeg($image);
  14. ?>
Copy the code

Example 2, draw interference line code for the picture:

  1. for($i=0;$i<8;$i++)//Draw multiple lines
  2. {
  3. $cg=imagecolorallocate($im,rand(0,255),rand (0,255),rand(0,255));//Generate random colors
  4. imageline($im,rand(10,40),0,rand(10,40),20,$cg);
  5. }
Copy Code

Example 3, code to draw interference points on pictures:

  1. for($i=0;$i<80;$i++)//Draw multiple points
  2. {
  3. imagesetpixel($im,rand(0,40),rand( 0,20),$cg);
  4. }
Copy code

Example 4, write text into image code:

  1. $str=array('i','i','pro','pro');//Storage displayed Chinese characters

  2. for($i= 0;$i<4;$i++)
  3. {
  4. $sss.=$str[rand(0,3)];//Display Chinese characters randomly
  5. }

  6. //$str= iconv("gb2312","utf-8",$str); //Chinese character encoding conversion, mine doesn’t seem to need it

  7. 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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn