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

Learn the simplest verification code production in php

WBOY
Release: 2016-07-29 08:58:16
Original
1046 people have browsed it

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template