-
-
//File header... - header("Content-type: image/png");
- //Create true color white paper
- $im = @imagecreatetruecolor (50, 20) or die("Failed to create image");
- //Get the background color
- $background_color = imagecolorallocate($im, 255, 255, 255);
- //Fill the background color (this thing is similar to an oil barrel)
- imagefill($im,0,0,$background_color);
- //Get the border color
- $border_color = imagecolorallocate($im,200,200,200);
- //Draw a rectangle, border color 200,200,200
- imagerectangle($im,0,0 ,49,19,$border_color);
//Show off the background line by line, use 1 or 0 for full screen
- for($i=2;$i<18;$i++){
- / /Get a random light color
- $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
- //Draw a line
- imageline($im,2,$i,47,$i, $line_color);
- }
//Set the font size
- $font_size=12;
//Set the printed text
- $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $Str[1] = "abcdefghijklmnopqrstuvwxyz";
- $Str[2] = "01234567891234567890123456";
//Get the first random text
- $imstr[0 ]["s"] = $Str[rand(0,2)][rand(0,25)];
- $imstr[0]["x"] = rand(2,5);
- $imstr[0 ]["y"] = rand(1,4);
//Get the second random text
- $imstr[1]["s"] = $Str[rand(0 ,2)][rand(0,25)];
- $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
- $ imstr[1]["y"] = rand(1,3);
//Get the 3rd random text
- $imstr[2]["s"] = $Str[ rand(0,2)][rand(0,25)];
- $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1) ;
- $imstr[2]["y"] = rand(1,4);
//Get the 4th random text
- $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
- $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0 ,1);
- $imstr[3]["y"] = rand(1,3);
//Write random string
- for($i=0;$i< ;4;$i++){
- //Get a random darker color
- $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
- //Draw text
- imagechar($im ,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);
- }
//Display image
- imagepng($im);
- //Destroy image
- imagedestroy($im);
- ?>
-
Copy code
Example 2, beautiful PHP image verification code example
Complete code:
-
- /*
- * @Author fy
- */
- $imgwidth =100; //Picture width
- $imgheight =40; //Picture height
- $codelen =4; //Verification code length
- $fontsize =20; //Font size
- $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
- $font = 'Fonts/segoesc.ttf';
- $im=imagecreatetruecolor($imgwidth,$imgheight);
- $while=imageColorAllocate($im,255,25 5,255 );
- imagefill($im,0,0,$while); //Fill the image
- //Get the string
- $authstr='';
- $_len = strlen($charset)-1;
- for ($i =0;$i<$codelen;$i++) {
- $authstr .= $charset[mt_rand(0,$_len)];
- }
- session_start();
- $_SESSION['scode']=strtolower($authstr );//Convert all to lowercase, mainly to avoid case sensitivity
- //Draw random dots, which have been changed to stars
- for ($i=0;$i<$imgwidth;$i++){
- $randcolor =imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight) , '*',$randcolor);
- //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
- }
- //Draw lines randomly, number of lines = characters Quantity (anything)
- for($i=0;$i<$codelen;$i++)
- {
- $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imageline ($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
- }
- $_x=intval($imgwidth/$codelen); //Calculate character distance
- $_y=intval($imgheight*0.7); //Characters are displayed at 70% of the image
- for($i=0;$i $randcolor=imagecolorallocate($im ,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
- //imagestring($im,5,$j,5,$imgstr[$i],$color3);
- // imagettftext ( resource $ image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
- imagettftext($im,$fontsize,mt_rand(-30,30),$i* $_x+3,$_y,$randcolor,$font,$authstr[$i]);
- }
- //Generate image
- header("content-type:image/PNG");
- imagePNG($im);
- imageDestroy($im);
Copy code Example 3, php5 image verification code example code
Example of php5 generating image verification code.
You need to use php GD library function:
1,imagecreatetruecolor-----Create a true color image
imagecreatetruecolor(int x_size,int y_size) //x represents width, y represents height
2. imagecolorallocate assigns colors (palette) to an image
imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----three primary colors
3. imagestring drawing function
iamgestring(resource image,font,int x,int y,content,color);
4. Output function
PHP's header is an action that defines the header. PHP5 supports 3 types:
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
Example: header("Content-type:image/jpeg")
There are corresponding image types in the GD library
imagejpeg(),imagegif(),imagepang()
5.imageline line drawing function
iamgeline(resource image,int x1,int y1,int x2,int y2,int color);
image ---picture
x1 ---starting coordinates
y1
x2 ---end point coordinates
y2
6.imagesetpixel drawing point function
imagesetpixel(resource image,int x,int y,int color)
7.imagettftext writing function with font
imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text)
8. How to insert Chinese php verification code
iconv("gb2312","utf-8","string"); //First convert the text into utf-8 format
9. Random function
1, rand([int min,int max]) //rand(1,4) generates numbers from 1 to 4
2, dechex (decimal number) //Convert to hexadecimal
Steps to generate verification code:
Generate random numbers -- create pictures -- write random numbers into pictures -- save in session
Enter verification code example
gdchek.php
- /*
- * Generate image verification code
- * and open the template in the editor.
- */
- session_start();
- for($i=0;$i<4; $i++){
- $rand.=dechex(rand(1,15)); //Generate a 4-digit random number containing hexadecimal
- }
- $_SESSION[check_gd]=$rand;
- $img=imagecreatetruecolor (100,30); //Create a picture
- $bg=imagecolorallocate($img,0,0,0); //The first time the background color is generated
- $fc=imagecolorallocate($img,255,255,255); // Generated font color
- //Draw lines on the picture
- for($i=0;$i<3;$i++){
- $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255 ));
- imageline($img,rand(0,15),0,100,30,$te);
- }
- //Draw some points on the picture
- for($i=0;$i<200;$i++){
- $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($img,rand()%100,rand()%30,$te);
- }
- //First, convert the text into utf-8 format
- //$str=iconv("gb2312","utf-8","Hehehe");
- //Add Chinese verification
- //smkai.ttf is In order to use a font file as a font on other people’s computers, put the file in the root directory of the project and download it. There is also
- imagettftext($img,11,10,20, 20,$fc,"simkai.ttf","Hello Hello");
- //Write the string in the image
- //imagestring($img,rand(1,6),rand(3,70) ,rand(3,16),$rand,$fc);
- //Output image
- header("Content-type:image/jpeg");
- imagejpeg($img);
- ?>
Copy code
login.php
|