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栈的定义、入栈出栈方法及基于堆栈实现的计算器。分享给大家供大家参考,具体如下: 栈是线性表的一种,他的特点是后入先出,可以这么理解,栈就像一个存东西的盒子,先放进去的...

PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例

本文实例讲述了PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能。分享给大家供大家参考,具体如下: <?php header("Content-Type:text...

php使用sql server验证连接数据库的方法

本文实例讲述了php使用sql server验证连接数据库的方法。分享给大家供大家参考。具体分析如下: 当您连接到 SQL Server 时,SQL Server Driver for...

PHP转换文件夹下所有文件编码的实现代码

PHP转换文件夹下所有文件的编码 适合发布网站的其他编码版本 比如你有一个GBK版本 你想有一个UTF8版本 或者你只有GBK的源码 你想二次开发 但是你不想改变IDE的编码方式 你可以...

php中Socket创建与监听实现方法

本文实例讲述了php中Socket创建与监听实现方法。分享给大家供大家参考。具体如下: 这里提供了在php中socket相关函数的一个实例,如创建Socket,接受一个socket连接...