Home  >  Article  >  Backend Development  >  h5 php5 image verification code implementation code

h5 php5 image verification code implementation code

WBOY
WBOYOriginal
2016-07-29 08:41:251211browse

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 color to an image (color adjustment board)
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
The header of php 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 represents the type of content file
For example: 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 - --Start coordinate
y1
x2 ---End coordinate
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 into PHP verification code
iconv("gb2312","utf-8","character String"); //First, convert the text into utf-8 format
9, random function
1, rand([int min, int max]) //rand(1,4) generates a number 1-4
2 , dechex (decimal number) //Convert to hexadecimal
Steps to make verification code:
Generate random numbers--Create pictures--Write random numbers into pictures--Save in session
Enter verification code example
gdchek.php

Copy the code The code is as follows:


/*
* 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 an image
$bg=imagecolorallocate($img,0,0,0); //The first time the background color is generated
$fc=imagecolorallocate( $img,255,255,255); //Generated font color
//Draw a line 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 Verify that
//smkai.ttf is a font file. In order to use it 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($ in C:WINDOWSFonts on this machine) 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);
?>


login.php

Copy code The code is as follows:


/*
*
*
*/
session_start();
if($_POST[sub ] ){
//Determine whether the verification code is the same
if($_POST[gd_pic]==$_SESSION[check_gd]){
echo "Verification successful! ";
}else{
echo "Verification code error";
}
}
?>


Username:

Password:

Verification code:


The above introduces the h5 php5 image verification code implementation code, including h5 content. I hope it will be helpful to friends who are interested in PHP tutorials.

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