Home  >  Article  >  Backend Development  >  php5 image verification code implementation code_PHP tutorial

php5 image verification code implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:35725browse

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 is a picture Image allocation color (palette)
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 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 with font The writing function
imagettftext(resource image, float size, float angle, int x, int y, int color, string fontfile, string text)
8, the method of inserting 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 a number from 1 to 4
2, dechex (decimal number) //Convert to hexadecimal
Steps for verification code:
Generate random number--Create Picture--random number written as picture--saved in session
Enter verification code example
gdchek.php

Copy 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 image
$bg=imagecolorallocate($img,0,0,0); //The first generation is the background color
$fc=imagecolorallocate($img,255,255,255); //The generated font color
//Draw lines for 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 dots 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 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($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 codes are the same
if($_POST[gd_pic]==$_SESSION[check_gd]){
echo "Verification successful! ";
}else{
echo "Verification code error";
}
}
?>

Username:

Password: < ;br>
Verification code:



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320947.htmlTechArticleGD library function 1, imagecreatetruecolor ----- Create a true color image imagecreatetruecolor(int x_size,int y_size) //x represents width, y represents height 2, imagecolorallocate divides an image...
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