php代码,主要把RGB改成随机生成,不是之前固定的七种颜色了:

 <?php
// ob_clean();
header("content-type:image/png");
$width = 110;
$height = 40;
$img = imagecreatetruecolor($width, $height);
$string = "hello";
//7种颜色,存入数组
$red = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$aaa = imagecolorallocate($img, 255, 255, 0);
$bbb = imagecolorallocate($img, 0, 255, 255);
$ccc = imagecolorallocate($img, 255, 0, 255);
$colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc);
//颜色换成随机组成的RGB,每次循环都生成一次
$color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
//画点
for ($i=0; $i < 10; $i++) {
$color1 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $color1);
}
//划线
for ($i=0; $i < 4; $i++) {
$color2 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $color2);
}
//生成4位验证码
$a1 = range(0, 9);
$a2 = range(a, z);
$a3 = range(A, Z);
$a4 = array_merge($a1,$a2,$a3);
//改用shuffle打断顺序,array_slice取出前4个字母数字。不然如果用mt_rand在循环中每次取一个,还要生成字符串,不好比对
shuffle($a4);
$a5 = array_slice($a4,0,4);
$num = 4;
$fontsize = 20;
for ($i=0; $i < 4; $i++) {
$color3 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imagettftext($img, $fontsize, mt_rand(-30,30), $width/$num*$i+5, 30, $color3, "Fonts/msyh.ttf", $a5[$i]);
}
imagepng($img);
?>

HTML代码(后缀也是php):

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="test.php" method = "get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text"><br/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text"><br/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name = "txtYanZheng"><br/></td>
</tr>
<tr>
<td></td>
<td><img src="yanzhengma.php" id = "yanzhengma" ></form></td>
</tr>
<tr>
<td><input type="submit" value="提交" ></td>
</tr>
</table>
<?php
$var = '
<script type="text/javascript">
onload = function(){
var yanzhengma = document.getElementById("yanzhengma");
yanzhengma.onclick = function(){
this.src = "yanzhengma.php?"+Math.random();
};
}
</script>
';
echo $var ?>
</body>
</html>

最新文章

  1. jQuery实现折叠下拉效果
  2. 【BZOJ-3165】Segment 李超线段树(标记永久化)
  3. 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别
  4. 整理课堂笔记 pl/sql orcale异常
  5. gulp学习
  6. easyui-datagrid 两次请求
  7. MongoDB在windows自启动
  8. POJ 1656
  9. Cloudera Manager5安装总结遇到问题及解决办法
  10. EasyGUI基础教程
  11. Android之HTTP网络通信--GET传递
  12. 关于hibernate中对象的三种状态分析
  13. 第二代map-reduce架构YARN解析
  14. WPF实现打印功能
  15. win10 uwp 视差效果
  16. css 单行文本居中显示,多行文本左对齐
  17. 织梦cms网上复制图片不可用的解决方法
  18. 理解主从设备模式(Master-Slave)
  19. SQL server存储过程,触发器,游标相关实例
  20. 什么是 js 变量提升 (Javascript Hoisting)

热门文章

  1. jstl中取map,其中map的key是一个对象,value是一个list
  2. javaWeb中RSA的加密使用
  3. Spring框架学习(7)spring mvc入门
  4. C#秘密武器之委托
  5. 从客户端的角度来谈谈移动端IM的消息可靠性和送达机制
  6. iOS 使用NSUserdefault 保存自定义的 对象
  7. STL源码剖析(list)
  8. UIKit class hierarchy
  9. 轻量级php框架phpk v1.0发布
  10. Calendar类经常用法 日期间的转换 set方法有巨坑