Home  >  Article  >  Backend Development  >  PHP image verification code production (Part 1)_PHP tutorial

PHP image verification code production (Part 1)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:48939browse

I am currently learning PHP. I am just getting started now, so many people don’t know it. So I will start from the most basic ones. If I don’t know how, I will check it online, and then write it down in this magic weapon, just like I encountered the random function rand today. (); I thought about what to do with it, and finally I remembered the verification code, numeric verification code, letter verification code, Chinese verification code, but I don’t know how to do it. What should I do? Search online and look at other people’s codes. If you don’t understand, watch the video, listen to the teacher, and take notes on the functions you encounter and the noteworthy points. Usually, you see random verification codes on general web pages surrounded by certain boxes. It looks like With picture as background.
After watching and typing by myself, although I encountered many problems that I didn't know, I believe that as long as I keep my feet on the ground, I will definitely learn it. Now I want to summarize it. My writing may be very messy, but I believe it will be realized one day.
1. Generate random numbers - "Create a picture -" Write the random number into the image - "Add interference values ​​(points, lines) to the image -" Keep it in the session - "Reference it in the form;
Random function: rand (int min, int max); it is inseparable from its origin. I have seen many codes for generating random numbers on the Internet, including random numbers with numbers and letters, Chinese random numbers (arrays), etc.; they are all inseparable. rand();
The code is as follows (some copied it online, I hope you won’t take it personally
The first type:

$authnum='';
$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";
$list=explode(",",$ychar);//Split function
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum];//Output in the form of array

The second type:

private function createCheckCode()
{
for(i=0;icodeNum;i++)
{
Number = rand(0,2);
       switch(number)
{
Case 0: rand_number = rand(48,57); break;//Number
Case 1: rand_number = rand(65,90);break;//uppercase letters
Case 2: rand_number = rand(97,122);break;//lowercase letters
}
$asc = sprintf("%c",rand_number);
$asc_number = asc_number.asc;
}
return asc_number;

}

The third type:
srand(microtime()*100000);//Equivalent to timer
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//Suddenly generate an array
}

The fourth type:
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//Convert decimal to hexadecimal
}
GD library: (Provides a series of IPI of 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") output function www.2cto.com
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
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 Chinese PHP verification code.
For the code, please see PHP Verification Code Production (Part 2)

Excerpted from ms.元

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478238.htmlTechArticleI am learning to get started with PHP recently. I am just getting started now, so many people don’t know it, so I will start from the most basic. If you don’t know how, look it up online, and then write it down in this magic weapon, just like when you encounter it today...
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