Home Backend Development PHP Tutorial Learn the simplest verification code production in php

Learn the simplest verification code production in php

Jul 29, 2016 am 08:58 AM
content img rand

The author is a computer student who is about to go out for an internship. PHP is purely my own hobby and I have learned some simple things by myself. Today I sent the verification code I made for everyone to give me some advice. Although it is simple, it took a lot of thought. The code is as follows:

<?php 
//定义图片格式
header("Content-type:image/png");

//定义画布大小,即验证码区域
$img=imagecreatetruecolor(80, 30);

//定义画笔颜色
$red1=imagecolorallocate($img, 0xff, 0x00, 0x00);
$green1=imagecolorallocate($img, 0x00, 0xff, 0x00);
$blue1=imagecolorallocate($img, 0x00, 0x00, 0xff);

//定义画布背景色
$bgcolor=imagecolorallocate($img, 0xff, 0xff, 0xff);

//将定义的颜色存入数组,以便随机换颜色
$col = array(&#39;0&#39; =>$red1,'1'=>$green1,'2'=>$blue1 );

//填充画布背景色
imagefill($img, 0, 0, $bgcolor);

//添加验证码内容
for($i=0;$i<4;$i++)
{
	$content .=&#39;&#39;; 
	$c
}
imagestring($img, 40, 20, 10, $content,$col[rand(0,2)] );


//添加干扰因素
	//添加干扰点
	for($i=0;$i<50;$i++)
	{
		imagesetpixel($img, rand(0,80), rand(0,40), $col[rand(0,2)]);
	}
	//添加干扰线
	for($j=0;$j<4;$j++)
	{	
Copy after login
		//imageline函数的格式:imageline(image, x1, y1, x2, y2, color);
		imageline($img, rand(0,20), rand(0,20), rand(0,80), rand(0,30), $col[rand(0,2)]);
	}

//输出图像

imagepng($img);

//释放图像资源
imagedestroy($img);
?>
Copy after login



The above introduces the simplest verification code production for learning PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks 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)

How to open img file How to open img file Sep 18, 2023 am 09:40 AM

Methods to open img files include using virtual optical drive software, using compression software, and using special tools. Detailed introduction: 1. Use virtual optical drive software to open, download and install a virtual optical drive software, right-click the img file, select "Open with" or "Associated Program", select the installed virtual optical drive software in the pop-up dialog box, virtual The optical drive software will automatically load the img file and use it as a disc image in the virtual optical drive. Double-click the disc icon in the virtual optical drive to open the img file and access its contents, etc.

How to solve 'undefined: rand.Seed' error in golang? How to solve 'undefined: rand.Seed' error in golang? Jun 25, 2023 am 08:34 AM

During the development or learning process of using Golang, we may encounter the error message of undefined:rand.Seed. This error usually occurs when you need to use a random number generator, because in Golang you need to set a random number seed before you can use the function in the rand package. This article will explain how to resolve this error. 1. Introduce the math/rand package. First, we need to introduce the math/rand package into the code. exist

How to open img file How to open img file Jul 06, 2023 pm 04:17 PM

How to open the img file: 1. Confirm the img file path; 2. Use the img file opener; 3. Select the opening method; 4. View the picture; 5. Save the picture. The img file is a commonly used image file format, usually used to store picture data.

What is the format of img? What is the format of img? Mar 17, 2023 am 10:33 AM

img is a file compression format, mainly used to create image files of floppy disks. It can be used to compress the contents of an entire floppy disk or an entire CD; files with the extension ".IMG" are created using this file format. ; The img file includes 3 basic nodes, namely "Ehfa_HeaderTag", "Ehfa_File" and "Ehfa_Entry".

How to synchronize random number generation in Golang parallel processing? How to synchronize random number generation in Golang parallel processing? Jun 03, 2024 pm 02:53 PM

Synchronizing random number generation in Go concurrent programming: Use a mutex (sync.Mutex) to control access to the rand.Rand random number generator. Each goroutine acquires the mutex lock before generating random numbers and releases the mutex lock after generating it. This ensures that only one goroutine can access the random number generator at a time, eliminating data races.

How to solve the problem of generating the same random numbers using php rand function How to solve the problem of generating the same random numbers using php rand function Mar 23, 2023 am 09:17 AM

The rand() function uses the same initial seeds on each call. The default initial seed is obtained from the operating system's time, but it only has microsecond accuracy. That is, within a very short period of time, many rand() function calls will use the same initial seed, resulting in the same random number generation. So, how to solve this problem?

U disk pe cannot read img U disk pe cannot read img Mar 19, 2024 pm 12:07 PM

U disk is one of the frequently used storage devices. There will also be some minor problems during use. For example: the U disk cannot be read. How to solve it? Put it away! Can’t read the USB flash drive? 4 steps to teach you how to solve the problem easily. After the USB flash drive has been inserted into the computer, click the Start menu, select Control Panel and put it away! Can’t read the USB flash drive? 4 steps to teach you to easily solve the problem. Find and double-click Device Manager in Control Panel. Keep it! Can’t read the USB flash drive? 4 steps to teach you how to easily solve the problem: Find the Universal Serial Bus controller on the main interface of the Device Manager and put it away! Can’t read the USB flash drive? 4 steps to teach you how to easily find the USB mass storage device, select it, right-click, find Uninstall and put it away! Can’t read the USB flash drive? 4 steps to teach you how to solve the problem easily. Welcome to leave a message for discussion!

How to change the src attribute of img in jquery How to change the src attribute of img in jquery Mar 03, 2022 pm 03:41 PM

How to change the img src attribute in jquery: 1. Use the attr() attribute, the syntax "$("img").attr("src","The address of the image file")"; 2. Use the prop() method, the syntax " $("img").prop("src","The address of the image file")".

See all articles