利用php生成验证码
2018-06-22 05:09:25来源:未知 阅读 ()

<?php
/**
* php生成验证码
* @param $width 画布宽
* @param $height 画布高
* @param $vcodelen 验证码长度
* @param $pointnum 干扰像素点数量
* @param $linenum 干扰线条数量
*
* 思路:创建验证码画布,生成并填充背景色,生成验证码内容/干扰像素点/线,填充到画布,输出。
*/
$width = 100;
$height = 30;
$vcodelen = 4;
$pointnum = 200;
$linenum = 3;
// 创建画布
$image = imagecreatetruecolor($width, $height);
// 创建色块
$bgcolor = imagecolorallocate($image, 255, 255, 255);
// 填充画布背景色
imagefill($image, 0, 0, $bgcolor);
// 验证码内容
for ($i=0; $i < $vcodelen; $i++) {
// 字体大小
$fontsize = 5;
// 字体颜色,颜色在限定范围内随机
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
$data = 'abcdefghijklmnopqrstuvwxyz0123456789';
// 验证码内容在以上字符串内随机截取
$fontcontent = substr($data, rand(0,strlen($data)),1);
// 字符串显示位置
$x = ($i*$width/4)+rand(5,15);
$y = rand(5,10);
// 字符串填充图片
// imagestring的字体大小可选1-5,字体再大需要用imagettftext函数(需要字体文件)
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
// imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, '/font/Geneva.dfont', $fontcontent);
}
// 干扰像素点
for ($i=0; $i < $pointnum; $i++) {
$pointcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
// 画布填充像素点函数
imagesetpixel($image, rand(0,$width), rand(0,$height), $pointcolor);
}
// 干扰线条
for ($i=0; $i < $linenum; $i++) {
$linecolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
// 画布填充线条函数
imageline($image, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $linecolor);
}
// 图片输出格式
header('content-type: image/png');
// 输出验证码图片
imagepng($image);
// 销毁画布
imagedestroy($image);
?>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
