php验证码功能源码分享
1、php原生代码实现验证码功能所需的函数:
1.header('Content-type:image/jpeg'):告诉浏览器输出的是图像。(一般放在最顶部,前面不能写入任何php代码)。
2.创建图像:imagecreatetruecolor();
3.填充颜色:imagecolorallocate();
4.区域填充:imagefill();
5.矩形函数:imagerectangle();
6.像素函数:imagesetpixel();
7.线段函数:imageline();
8.数组函数:array();
9.图像写入文本函数:imagettftext();
10.输出图像函数:imagejpeg();
11.释放图像内存函数:imagedestroy();
2、首先头部header函数声明输出的是图像而不是文本
header('Content-type:image/jpeg');
3、创建图像:
$width = 120;
$height = 40;
$img = imagecreatetruecolor($width, $height);
4、创建多个颜色代码:背景颜色,边框颜色,背景杂色颜色,验证码字符颜色
背景颜色
$colorBg = imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
边框颜色,随机
$colorBorder = imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
背景小点颜色
$colorDian = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
验证码字符颜色
$colorString = imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
5、验证码背景颜色区域填充:
imagefill($img, 0, 0, $colorBg);
6、画一个矩形
imagerectangle($img, 0,0, $width-1, $height-1, $colorBorder);
7、画多个小点,做为背景杂色
for($i=0;$i<500;$i++){
imagesetpixel($img, rand(0,$width-1), rand(0,$height-1),$colorDian );
}
8、画多条线段
for($y=0;$y<5;$y++){
imageline($img, rand(0,$width/4), rand(0,$height-1), rand($width/4*3,$width-1), rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
};
9、验证码字符串数组
$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');
$element_ = '';
for($i=0;$i<4;$i++){
$element_ =$element_ . $element[rand(0,count($element)-1)];
}
10、图像写入文本函数
imagettftext($img, 34, rand(-5,5), 5, rand(30,39), $colorString, 'font/yanzhengma2.otf', $element_);
11、输出图像
imagejpeg($img);
12、释放图像内存
imagedestroy($img);
13、完整代码:
<?php
//验证码实现
header('Content-type:image/jpeg');
$width = 120;
$height = 40;
//创建图像
$img = imagecreatetruecolor($width, $height);
//背景颜色
$colorBg = imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
//边框颜色,随机
$colorBorder = imagecolorallocate($img, rand(200,255), rand(200,255), rand(200,255));
//背景小点颜色
$colorDian = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
//验证码字符颜色
$colorString = imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
//区域填充
imagefill($img, 0, 0, $colorBg);
//画一个矩形
imagerectangle($img, 0,0, $width-1, $height-1, $colorBorder);
//画多个小点
for($i=0;$i<500;$i++){
imagesetpixel($img, rand(0,$width-1), rand(0,$height-1),$colorDian );
}
//画一条线段
for($y=0;$y<5;$y++){
imageline($img, rand(0,$width/4), rand(0,$height-1), rand($width/4*3,$width-1), rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
};
//验证码字符串数组
$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');
$element_ = '';
for($i=0;$i<4;$i++){
$element_ =$element_ . $element[rand(0,count($element)-1)];
}
//验证码字符串
/*
不用这个函数
imagestring($img, 5, 0, 0, 'abcd', $colorString);
*/
//图像写入文本函数
imagettftext($img, 34, rand(-5,5), 5, rand(30,39), $colorString, 'font/yanzhengma2.otf', $element_);
//输出图像
imagejpeg($img);
//释放图像内存
imagedestroy($img);
?>
14、效果