Php5 image verification code implementation code

  • 2020-03-31 20:14:27
  • OfStack

Function of GD library
1,imagecreatetruecolor -- create a truecolor image
Imagecreatetruecolor (int x_size,int y_size) //x for width and y for height
2, imagecolorallocate the color (palette) for 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 the action of defining the header. Php5 supports 3 types:
1, the content-type: XXXX/yyyy
2, the Location: XXXX: yyyy/ZZZZ
3, the Status: NNN XXXXXX
XXXX /yyyy indicates the type of content file
Such as: image/GIF
Image/jpeg
Image/PNG
Example: the header (" content-type: image/jpeg)"
The GD library has the corresponding image type
Imagegif imagejpeg (), (), imagepang ()
5. Imageline drawing function
Iamgeline (resource image,int x1,int y1,int x2,int y2,int color);
Image - image
X1 -- the starting coordinate
y1
X2 -- the endpoint
y2
6. Image pixel dot function
Imagesetpixel (resource image,int x,int y,int color)
7,imagettftext with font write function
Imagettftext (resource image,float size,float Angle,int x,int y,int color,string fontfile,string text)
8, PHP verification code insert Chinese method
Iconv ("gb2312","utf-8"," string "); // first convert the text to utf-8 format
9. Random functions
1, rand([int min,int Max]) //rand(1,4) generates 1-4
2, dechex // to hexadecimal
Steps to do verification code:
Generate a random number -- create a picture -- write the random number as a picture -- and store it in session
Enter a sample verification code
Gdchek. PHP
 
<?php 
 
session_start(); 
for($i=0;$i<4;$i++){ 
$rand.=dechex(rand(1,15)); //Generate a random number with four digits in hexadecimal
} 
$_SESSION[check_gd]=$rand; 
$img=imagecreatetruecolor(100,30); //Create a picture
$bg=imagecolorallocate($img,0,0,0); //The first is the background color
$fc=imagecolorallocate($img,255,255,255); //Generated font color
//Underline 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 a little bit of a 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 to utf-8 format
//$STR =iconv("gb2312","utf-8"," hehehe ");
//Add validation in Chinese
//Smkai-ttf is a font file. To make it work on someone else's computer, put the file in the project's root directory, which you can download, as well as native C: WINDOWS Fonts
imagettftext($img,11,10,20,20,$fc,"simkai.ttf"," Hello how are you "); 
//Write the string in the picture
//imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc); 
//The output image
header("Content-type:image/jpeg"); 
imagejpeg($img); 
?> 

The login. PHP
 
<?php 
 
session_start(); 
if($_POST[sub]){ 
//Determine if the captcha is the same
if($_POST[gd_pic]==$_SESSION[check_gd]){ 
echo " Verify successful! "; 
}else{ 
echo " Verification code error "; 
} 
} 
?> 
<form action="login.php" method="POST"> 
 User name: <input type="text" name="user"/><br> 
 Password: <input type="password" name="pwd"/><br> 
 Verification code: <imput type="text" name="gd_pic"/><img src="gdchek.php"><br> 
<imput type="submit" name="sub" value="submit"/> 
</form> 

Related articles: