<?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. hdu 5040bfs+优先队列 需要存状态

    /* 剪枝:四秒后状态会变得和原来一样,所以四秒后如果再经过这个点肯定不是最优的舍去 易错点:在一个是从.到.这两个点都没有被照到并且不是摄像机,也可能需要等3秒,因为后面的结果可能再这三秒中发生改变 ...

  2. poj 2823单调队列模板题

    #include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N  1100000 int getmin[N],getmax[N],num[N],n,k, ...

  3. Spring @Conditional注解 详细讲解及示例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xcy1193068639/article/details/81491071 前言: @Conditi ...

  4. linux下安装并配置vim

    1.安装:sudo apt-get install vim-gtk  安装好后vim,并按“tab”键,可以看到vim的存在,则安装好2.设置更加人性化:sudo vim /etc/vim/vimrc ...

  5. codevs2486 太鼓达人

    题目描述 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk.Poet_s ...

  6. Hihocoder 1325 (splay)

    Problem 平衡树 Treap 题目大意 维护一个数列,支持两种操作. 操作1:添加一个数x. 操作2:询问不超过x的最大的数. 解题分析 尝试了一下用指针来写splay,感觉写起来还是比较流畅的 ...

  7. 线程间的通信----wait/notify机制

    wait/notify机制 实现多个线程之间的通信可以使用wait.notify.notifyAll三个方法.这三个方法都是Object类的方法.wait():导致当前线程等待,直到另一个线程调用此对 ...

  8. 1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.

    package com.code; public class Test04_3 { public static int solution(int X, int[] A) { int size = A. ...

  9. ansible安装测试

    安装: # yum install ansible # yum install sshpass 配置: # vi /etc/ansible/hosts  [mysqldb] 172.16.100.23 ...

  10. 0x0118消息就是WM_SYSTIMER

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/c0f9bac9-d211-4b8b-ba99-f5a0ed0d2e0a/what-is-w ...