php 生成随机验证码图片代码

yipeiwu_com6年前PHP代码库

复制代码 代码如下:

<?php
/** 默认首页 **/
class DefaultController extends AppController
{
public function index() {
$len = 5;
$str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789";

$im = imagecreatetruecolor ( 70, 20 );
$bgc = imagecolorallocate($im, 255, 255, 255);
$bgtxt = imagecolorallocate($im, 220, 220, 220);

//随机调色板
$colors = array(
imagecolorallocate($im, 255, 0, 0),
imagecolorallocate($im, 0, 200, 0),
imagecolorallocate($im, 0, 0, 255),
imagecolorallocate($im, 0, 0, 0),
imagecolorallocate($im, 255, 128, 0),
imagecolorallocate($im, 255, 208, 0),
imagecolorallocate($im, 98, 186, 245),
);

//填充背景色
imagefill($im, 0, 0, $bgc);

//随机获取数字
$verify = "";
while (strlen($verify) < $len) {
$i = strlen($verify);
$random = $str[rand(0, strlen($str))];
$verify .= $random;

//绘制背景文字
imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $bgtxt);
//绘制主文字信息
imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $colors[rand(0, count($colors)-1)]);
}

//添加随机杂色
for($i=0; $i<100; $i++) {
$color = imagecolorallocate($im, rand(50,220), rand(50,220), rand(50,220));
imagesetpixel($im, rand(0,70), rand(0,20), $color);
}

//将验证码存入$_SESSION中
sess("verify", $verify);

//输出图片并释放缓存
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
}
};
?>

相关文章

解析PHP无限级分类方法及代码

解析PHP无限级分类方法及代码

无论你要构建自己的论坛,在你的网站上发布消息还是书写自己的CMS程序,你都会遇到要在数据库中存储层次数据的情况。同时,除非你使用一种像XML的数据库,否则关系数据库中的表都不是层次结构的...

PHP 模板高级篇总结

如何使用PHP来快速地编写代码,模版似乎成了唯一的选择。但是一个PHPer最终应该坚持使用模版,放弃模版,还是使用自己的模版?     以下想法是...

phpMyAdmin出现无法载入 mcrypt 扩展,请检查PHP配置的解决方法

1、没有正确安装Mysql数据库,在系统服务中Mysql相关的服务没有启动 (请查看正确安装Mysql的方法) 2、在系统的 system32(C:\windows\system32)...

解析isset与is_null的区别

isset和is_null啥区别,看手册上讲的话, isset和is_null的功能几乎完全”相反的一样”..是不是isset就是一个is_null的相反的别名?诶, 要说区别, 那还真...

PHP使用DOM和simplexml读取xml文档的方法示例

本文实例讲述了PHP使用DOM和simplexml读取xml文档的方法。分享给大家供大家参考,具体如下: 实例  用DOM获取下列xml文档中所有金庸小说的书名,该xml文档所...