<?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);
?>

利用php生成验证码的更多相关文章

  1. 利用kaptcha生成验证码的详细教程

    kaptcha是一个简单好用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.Ka ...

  2. 利用django-simple-captcha生成验证码

    参考文档 http://django-simple-captcha.readthedocs.io/en/latest/ django支持1.7+ 1.安装 pip install django-sim ...

  3. Javaweb中利用kaptcha生成验证码

    引入kaptcha-2.3-jdk15.jar包 在web.xml中进行配置 <servlet> <servlet-name>Kaptcha</servlet-name& ...

  4. 利用PIL创建验证码

    1. 随机生成rgb 元组 def random_RGB(min, max): return tuple([random.randint(min, max) for i in range(3)])2. ...

  5. 学习笔记:利用GDI+生成简单的验证码图片

    学习笔记:利用GDI+生成简单的验证码图片 /// <summary> /// 单击图片时切换图片 /// </summary> /// <param name=&quo ...

  6. Python利用PIL生成随机验证码图片

    安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...

  7. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  8. PHP利用jquery生成各种验证码和Ajax验证

    PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...

  9. 利用random模块生成验证码

    random模块 该模块用于数学或者数据相关的领域,使用方法非常简单下面介绍常用的放法 1.随机小数 random.random() 2.随机整数random.randint(1,5) # 大于等于1 ...

随机推荐

  1. MySQL的Date()函数拼接

    SELECT date_format(DATE(a.date_created),'%Y-%c-%d') As dateCreate, SUM(a.sm) As sumAmount, sum(order ...

  2. linux命令与技巧

    1.模糊查询:find / -name '*Eclipse*'2.获得管理员权限:sudo -i

  3. T1081 线段树练习 2 codevs

    http://codevs.cn/problem/1081/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数, ...

  4. Linux 安装 RabbitMQ

    转载文章,地址:https://www.cnblogs.com/uptothesky/p/6094357.html 侵删!

  5. iOS消息推送原理和实现总结

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图:1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provi ...

  6. Oracle Multitenant Environment (二) Purpose

    Purpose of a Multitenant Environment A multitenant environment enables the central management of mul ...

  7. 大家好 这个事我的BLOG 站点 欢迎大家 訪问和公布文章技术的 和评论 交流技术使用

    地址 http://microlmj.gotoip3.com/blog/article!showAllArticleForPageTest.action ssh+mysql+java+tomcat+b ...

  8. Parallel and Perpendicular

    题目链接 题意: 输入n,求正n边形中的对角线1和对角线2的个数(对角线1:至少与其它一个对角线平行:对角线2:至少与其它一个对角线垂直).对角线不能是多边形的边 (4 ≤ n ≤ 10e5) 分析: ...

  9. cgi程序读取post发送的特殊字符,尤其适合于微信公众平台开发中发送被动消息

    [问题]用c编写cgi程序怎样取出html表单post来的数据? [分析]html表单post来的数据形如username="zhang"&&password=&q ...

  10. 【树剖求LCA】树剖知识点

    不太优美但是有注释的版本: #include<cstdio> #include<iostream> using namespace std; struct edge{ int ...