php生成图形验证码几种方法小结


Posted in PHP onAugust 15, 2013

我们先来检查一下自己的php是不是打开了gd库。

<?php
if(extension_loaded('gd')) {
  echo '你可以使用gd<br>';
  foreach(gd_info() as $cate=>$value)
    echo "$cate: $value<br>";
}else
  echo '你没有安装gd扩展';
?>

如果有返回信息就正确可以常用使用了
例1
<?php
/**
 * vCode(m,n,x,y) m个数字  显示大小为n   边宽x   边高y
 * 自己改写记录session $code
 */
session_start(); 
vCode(4, 15); //4个数字,显示大小为15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
 !$width && $width = $num * $size * 4 / 5 + 5;
 !$height && $height = $size + 10; 
 // 去掉了 0 1 O l 等
 $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
 $code = '';
 for ($i = 0; $i < $num; $i++) {
  $code .= $str[mt_rand(0, strlen($str)-1)];
 } 
 // 画图像
 $im = imagecreatetruecolor($width, $height); 
 // 定义要用到的颜色
 $back_color = imagecolorallocate($im, 235, 236, 237);
 $boer_color = imagecolorallocate($im, 118, 151, 199);
 $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); 
 // 画背景
 imagefilledrectangle($im, 0, 0, $width, $height, $back_color); 
 // 画边框
 imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color); 
 // 画干扰线
 for($i = 0;$i < 5;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
 } 
 // 画干扰点
 for($i = 0;$i < 50;$i++) {
  $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
 } 
 // 画验证码
 @imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\WINDOWS\Fonts\simsun.ttc', $code);
 $_SESSION["VerifyCode"]=$code; 
 header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
 header("Content-type: image/png;charset=gb2312");
 imagepng($im);
 imagedestroy($im);
}
?>

例2
使用PHP,结合session和GD库扩展开发的一个生成验证码的例子(w3c推荐),可以很方便的用于项目中。而且样式美观
//首先开启session
session_start();
//定义前台显示验证码长&宽
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 10;
$random_lines = 30;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
//定义要生成验证码的字符串
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
        $arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
        $arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//设置session,做验证
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);
  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
}

个人推荐推荐第二个生成验证码程序代码,各位同学可尝试参考对比哦,最后一个是W3C标准生成的也是利用了php gd库。
PHP 相关文章推荐
php将mysql数据库整库导出生成sql文件的具体实现
Jan 08 PHP
一个简洁实用的PHP缓存类完整实例
Jul 26 PHP
PHP中file_exists()判断中文文件名无效的解决方法
Nov 12 PHP
php操作(删除,提取,增加)zip文件方法详解
Mar 12 PHP
php导入模块文件分享
Mar 17 PHP
php实现的简单美国商品税计算函数
Jul 13 PHP
简单了解PHP编程中数组的指针的使用
Nov 30 PHP
PHP简单判断字符串是否包含另一个字符串的方法
Mar 25 PHP
php 在字符串指定位置插入新字符的简单实现
Jun 28 PHP
Laravel中基于Artisan View扩展包创建及删除应用视图文件的方法
Oct 08 PHP
php使用include 和require引入文件的区别
Feb 16 PHP
PHP网页安全认证的实例详解
Sep 28 PHP
PHP中将ip地址转成十进制数的两种实用方法
Aug 15 #PHP
PHP怎么实现网站保存快捷方式方便用户随时浏览
Aug 15 #PHP
php正则取img标记中任意属性(正则替换去掉或改变图片img标记中的任意属性)
Aug 13 #PHP
php使用sql数据库 获取字段问题介绍
Aug 12 #PHP
完美解决PHP中的Cannot modify header information 问题
Aug 12 #PHP
php 下载保存文件保存到本地的两种实现方法
Aug 12 #PHP
CodeIgniter上传图片成功的全部过程分享
Aug 12 #PHP
You might like
ThinkPHP模板判断输出Defined标签用法详解
2014/06/30 PHP
ThinkPHP采用实现三级循环代码实例
2014/07/18 PHP
php实现统计目录文件大小的函数
2015/12/25 PHP
Netbeans 8.2将支持PHP7 更精彩
2016/06/13 PHP
PHP入门教程之会话控制技巧(cookie与session)
2016/09/11 PHP
自写的一个jQuery圆角插件
2010/10/26 Javascript
js读写cookie实现一个底部广告浮层效果的两种方法
2013/12/29 Javascript
js的Boolean对象初始值示例
2014/03/04 Javascript
用JavaScript实现类似于ListBox功能示例代码
2014/03/09 Javascript
基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码
2014/05/11 Javascript
php读取sqlite数据库入门实例代码
2014/06/25 Javascript
jQuery javascript获得网页的高度与宽度的实现代码
2016/04/26 Javascript
Angular和百度地图的结合实例代码
2016/10/19 Javascript
jQuery Dialog 打开时自动聚焦的解决方法(两种方法)
2016/11/24 Javascript
JavaScript中清空数组的方法总结
2016/12/02 Javascript
jQuery Easyui加载表格出错时在表格中间显示自定义的提示内容
2016/12/08 Javascript
jQuery鼠标悬停内容动画切换效果
2017/04/27 jQuery
JS 学习总结之正则表达式的懒惰性和贪婪性
2017/07/03 Javascript
总结js中的一些兼容性易错的问题
2017/12/18 Javascript
Angular6 正则表达式允许输入部分中文字符
2018/09/10 Javascript
[01:38]DOTA2 2015国际邀请赛中国区预选赛 Showopen
2015/06/01 DOTA
[01:04:02]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第二场 1月24日
2021/03/11 DOTA
Python列表append和+的区别浅析
2015/02/02 Python
详解Django中的过滤器
2015/07/16 Python
pandas的排序和排名的具体使用
2019/07/31 Python
浅析python,PyCharm,Anaconda三者之间的关系
2019/11/27 Python
Python threading模块condition原理及运行流程详解
2020/10/05 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
电气自动化个人求职信范文
2014/02/03 职场文书
会计人员岗位职责
2014/03/19 职场文书
解除合同协议书
2014/04/17 职场文书
三月学雷锋月活动总结
2014/04/28 职场文书
答谢会策划方案
2014/05/12 职场文书
小学运动会口号
2014/06/07 职场文书
Python代码风格与编程习惯重要吗?
2021/06/03 Python
Win10开机修复磁盘错误怎么跳过?Win10关闭开机磁盘检查的方法
2022/09/23 数码科技