Home Backend Development PHP Tutorial Example of making PHP image verification code

Example of making PHP image verification code

Jul 25, 2016 am 08:55 AM

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

Method 2, defined as a private function.

  1. private function createCheckCode()
  2. {
  3. for(i=0;icodeNum;i++)
  4. {
  5. number = rand(0,2);
  6. switch( number)
  7. {
  8. case 0: rand_number = rand(48,57); break;//Number
  9. case 1: rand_number = rand(65,90);break;//Capital letters
  10. case 2: rand_number = rand(97,122 );break;//Lowercase letters
  11. }
  12. $asc = sprintf("%c",rand_number);
  13. $asc_number = asc_number.asc;
  14. }
  15. return asc_number;
  16. }
Copy code

Method 3 , use random seeds to generate PHP verification codes.

  1. srand(microtime()*100000);//Equivalent to timer
  2. $string="abcdefghigklmnopqrstuvwxyz123456789";
  3. for($i=0;$i<4;$i++)
  4. {
  5. $new_number.=$string[rand(0,strlen($string)-1)];//Immediately generate an array
  6. }
Copy code

Method 4,

  1. for($i=0;$i<4;$i++)
  2. {
  3. $rand.=dechex(rand(1,15));//Convert decimal to hexadecimal
  4. }
Copy the code

Then let’s get to the focus of this article: PHP GD library: (Provides a series of IPIs for 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") The header of the output function php is an action that defines the header. php5 supports 3 of them. Type: 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 the PHP verification code into Chinese. Here you can refer to the method of generating PHP Chinese verification code.

Example 1, randomly generate numbers and letter codes:

  1. //che.php
  2. session_start();
  3. for($i=0;$i<4;$i++)
  4. {
  5. $rand.=dechex(rand(1, 15));
  6. }
  7. $_SESSION['check_num']=$rand;
  8. $image=imagecreatetruecolor(50,30);
  9. $bg=imagecolorallocate($im,0,0,0);//First time When using the palette, the background color
  10. $te=imagecolorallocate($im,255,255,255);
  11. imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
  12. ob_clean();//The image "http://localhost/**.php" appears in the PHP webpage because it needs to generate a verification code because it has errors and cannot be displayed
  13. header("Content-type:image/jpeg") ; imagejpeg($image);
  14. ?>
Copy the code

Example 2, draw interference line code for the picture:

  1. for($i=0;$i<8;$i++)//Draw multiple lines
  2. {
  3. $cg=imagecolorallocate($im,rand(0,255),rand (0,255),rand(0,255));//Generate random colors
  4. imageline($im,rand(10,40),0,rand(10,40),20,$cg);
  5. }
Copy Code

Example 3, code to draw interference points on pictures:

  1. for($i=0;$i<80;$i++)//Draw multiple points
  2. {
  3. imagesetpixel($im,rand(0,40),rand( 0,20),$cg);
  4. }
Copy code

Example 4, write text into image code:

  1. $str=array('i','i','pro','pro');//Storage displayed Chinese characters

  2. for($i= 0;$i<4;$i++)
  3. {
  4. $sss.=$str[rand(0,3)];//Display Chinese characters randomly
  5. }

  6. //$str= iconv("gb2312","utf-8",$str); //Chinese character encoding conversion, mine doesn’t seem to need it

  7. imagettftext($im,10,0,rand(5,60),rand(5,60) ,$te,"simhei.ttf",$sss);//

Copy code

0: the inclination of the font, "simhei.ttf": the font style, usually placed at the root Under the directory;

This completes the entire production process of PHP image verification code. The script editor hopes that this article will be helpful to use the gd library to generate PHP verification code.



Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles