PHP 生成图像验证码,个性化参数
来源:互联网 发布:决战武林羽扇6升7数据 编辑:程序博客网 时间:2024/05/21 07:46
<?php// 产生一个随机字符串function buildRandomString($type = 1, $length = 4) { if ($type == 1) { $chars = join("", range(0, 9)); } elseif ($type == 2) { // array_merge 把两个数组合并为一个数组: $chars = join("", array_merge(range("A", "Z"), range("A", "Z"))); } elseif ($type == 3) { $chars = join("", array_merge(range("A", "Z"), range(0, 9))); } elseif ($type == 4) { $chars = join("", array_merge(range("a", "z"), range("A", "Z"), range(0, 9))); } if ($length > strlen($chars)) { exit("字符串长度不够"); } // 随机地打乱字符串中的所有字符: $chars = str_shuffle($chars); // 截取字符串 return substr($chars, 0, $length);}// 使用GD库做验证码function verifyImage($type = 1, $length = 4, $pixel = 1, $line = 0) { // session_start(); $width = 100; // 画布宽 $height = 40; // 画布高 $image = imagecreatetruecolor($width, $height); // 创建验证码画布 $black = imagecolorallocate($image, 0x00, 0x00, 0x00); $green = imagecolorallocate($image, 0x00, 0xFF, 0x00); $blue = imagecolorallocate($image, 0x00, 0x00, 0xFF); $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); $random_color = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); // 随机颜色 $fontfiles = array("FZSTK.TTF", "msyh.ttc", "msyhbd.ttc", "msyhl.ttc", "simsun.ttc", "SIMYOU.TTF", "STXINGKA.TTF"); // 随机字体 // 矩形区域着色 imagefill($image, 0, 0, $white); // imagefilledrectangle($image, 0, 0, $width, $height, $white); // 产生一个随机字符串 // $type = 1; // 验证码类型 // $length = 4; // 验证码长度 $chars = buildRandomString($type, $length); // 保存字符串,以待验证 $sess_name = "verify"; $_SESSION[$sess_name] = $chars; // 将字符串写到图片中 for ($i = 0; $i < $length; $i ++) { $size = mt_rand(20, 25); // 字符字号大小 $angle = mt_rand(- 15, 15); // 旋转角度 $x = 5 + $i * $size; // x y 两参数为文字的坐标值 (原点为左上角) $y = mt_rand(28, 38); $color = $random_color; $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); // 写 TTF 文字到图中。 } // 加入噪点干扰(可选) // $pixel = true; if ($pixel) { for($i = 0; $i < 30; $i ++) { imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $black); imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $green); imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $blue); } } // 加入直线干扰(可选) // $line = false; if ($line) { for($i = 0; $i < 1; $i ++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $black); imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $green); imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $blue); } } // 输出验证码 ob_clean(); header("content-type: image/png"); imagepng($image); imagedestroy($image);}verifyImage(1, 4, 1, 0);
0 0
- PHP 生成图像验证码,个性化参数
- PHP生成图像验证码
- php封装一个生成图像验证码
- 使用PHP生成带LOGO的个性化二维码图像
- 生成验证码图像
- 利用PHP应用图像处理生成验证码
- PHP使用GD库生成图像验证码
- PHP图形操作之生成图像验证码
- PHP生成图像验证码(GD库的使用)
- 验证码.图像的生成
- JSP生成验证码图像
- PHP生成验证码
- PHP 生成验证码
- PHP验证码生成
- PHP生成验证码
- php 生成验证码
- php 生成验证码
- php:生成验证码
- day1
- Linux之V4L2基础编程
- java正则表达式
- 任务分配问题(匈牙利算法)
- qcy教你下载你在CSDN上写过的文章
- PHP 生成图像验证码,个性化参数
- 转载:基于C语言中野指针的深入解析
- WBMP图片格式和转换JPEG方法
- Linux下安装 jdk-7u55-linux-i586.tar.gz并验证jdk安装成功
- DataFrame执行groupby聚合操作后,如何继续保持DataFrame对象而不变成Series对象
- 二叉搜索树
- 基于 Django1.10 文档的深入学习(16)——Authentication backends 之 class ModelBackend
- linux内核空间内存管理基本架构
- utf-8中的汉字占多少个字节