使用PHP制作简单的图形验证码
1、使用PHP编写图形验证码的思路
- 首先设定图形验证码的宽和高,以及验证码的可选值范围;
- 创建图像,设置背景颜色,验证码的字体颜色,并填充图像;
- 设置像素点和验证码上的显示的线条;
- 设置验证码的显示位置和字形等;
- 输出图像,释放资源;
2、实现的功能
- 刷新时,图形验证码的背景颜色,字体颜色,字体位置等会发生变化;
3、实现的过程
源码如下:
<?php
header('Content-type:image/jpeg');
//设置验证码图像的宽和高
$width=120;
$height=40;
//验证码可选值
$element=array('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');
//验证码的值
$string='';
for ($i=0;$i<5;$i++){
$string.=$element[rand(0,count($element)-1)];
}
//创建图像;
$img=imagecreatetruecolor($width, $height);
//设置图像验证码的背景颜色
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
//设置验证码的字体颜色
$colorString=imagecolorallocate($img,rand(10,50),rand(10,50),rand(10,50));
//给图片填充背景颜色,从左上角开始填充
imagefill($img,0,0,$colorBg);