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 相关文章推荐
PHP5在Apache下的两种模式的安装
Sep 05 PHP
php strtotime 函数UNIX时间戳
Jan 14 PHP
ubuntu 编译安装php 5.3.3+memcache的方法
Aug 05 PHP
PHP OPCode缓存 APC详细介绍
Oct 12 PHP
php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法
Nov 26 PHP
屏蔽机器人从你的网站搜取email地址的php代码
Nov 14 PHP
使用php记录用户通过搜索引擎进网站的关键词
Feb 13 PHP
跟我学Laravel之请求与输入
Oct 15 PHP
php自定义urlencode,urldecode函数实例
Mar 24 PHP
php编写的一个E-mail验证类
Mar 25 PHP
PHP Cookie学习笔记
Aug 23 PHP
PHP实现负载均衡的加权轮询方法分析
Aug 22 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
备份mysql数据库的php代码(一个表一个文件)
2010/05/28 PHP
php 数组的指针操作实现代码
2011/02/08 PHP
php中Smarty模板初体验
2011/08/08 PHP
采集邮箱的php代码(抓取网页中的邮箱地址)
2012/07/17 PHP
通过PHP自带的服务器来查看正则匹配结果的方法
2015/12/24 PHP
PHP使用Redis实现防止大并发下二次写入的方法
2017/10/09 PHP
由prototype_1.3.1进入javascript殿堂-类的初探
2006/11/06 Javascript
javascript[js]获取url参数的代码
2007/10/17 Javascript
初窥JQuery(二) 事件机制(1)
2010/11/25 Javascript
jQuery实现可收缩展开的级联菜单实例代码
2013/11/27 Javascript
JQuery设置获取下拉菜单某个选项的值(比较全)
2014/08/05 Javascript
全面理解JavaScript中的继承(必看)
2016/06/16 Javascript
vue.js绑定class和style样式(6)
2016/12/09 Javascript
JS实现自定义状态栏动画文字效果示例
2017/10/12 Javascript
vue 动态绑定背景图片的方法
2018/08/10 Javascript
Vue+tracking.js 实现前端人脸检测功能
2020/04/16 Javascript
vue实现公共方法抽离
2020/07/31 Javascript
Python编码爬坑指南(必看)
2016/06/10 Python
解决pycharm 误删掉项目文件的处理方法
2018/10/22 Python
解决PyCharm的Python.exe已经停止工作的问题
2018/11/29 Python
windows下搭建python scrapy爬虫框架步骤
2018/12/23 Python
简单了解python单例模式的几种写法
2019/07/01 Python
Python selenium键盘鼠标事件实现过程详解
2020/07/28 Python
python简单实现插入排序实例代码
2020/12/16 Python
python switch 实现多分支选择功能
2020/12/21 Python
6PM官网:折扣鞋、服装及配饰
2018/08/03 全球购物
外贸实习生自荐信范文
2013/11/24 职场文书
受欢迎的大学生自我评价
2013/12/05 职场文书
应聘文员自荐信范文
2014/03/11 职场文书
元宵节主持词
2014/03/25 职场文书
企业理念标语
2014/06/09 职场文书
2014年团员学习十八大思想汇报
2014/09/13 职场文书
党的群众路线教育实践活动查摆剖析材料
2014/10/10 职场文书
幼儿园教学工作总结2015
2015/05/12 职场文书
中小企业员工手册范本
2015/05/14 职场文书
适合后台管理系统开发的12个前端框架(小结)
2021/06/29 Javascript