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修改时间格式的代码
May 29 PHP
初学PHP的朋友 经常问的一些问题。不断更新
Aug 11 PHP
PHP面向对象程序设计之类常量用法实例
Aug 20 PHP
php解析xml方法实例详解
May 12 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
Sep 22 PHP
分享五个PHP7性能优化提升技巧
Dec 07 PHP
Yii中CArrayDataProvider和CActiveDataProvider区别实例分析
Mar 02 PHP
PHP文件上传类实例详解
Apr 08 PHP
php中Redis的应用--消息传递
Mar 28 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
Jun 07 PHP
php实现往pdf中加数字签名操作示例【附源码下载】
Aug 07 PHP
PHP大文件切割上传并带进度条功能示例
Jul 01 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
PHP 已经成熟
2006/12/04 PHP
10条PHP编程习惯助你找工作
2008/09/29 PHP
我的php学习笔记(毕业设计)
2012/02/21 PHP
排序算法之PHP版快速排序、冒泡排序
2014/04/09 PHP
检测codeigniter脚本消耗内存情况的方法
2015/03/21 PHP
php简单实现短网址(短链)还原的方法(测试可用)
2016/05/09 PHP
PHP识别二维码的方法(php-zbarcode安装与使用)
2016/07/07 PHP
CI框架实现框架前后端分离的方法详解
2016/12/30 PHP
PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
2019/11/04 PHP
JSON 编辑器实现代码
2009/12/06 Javascript
EasyUI的treegrid组件动态加载数据问题的解决办法
2011/12/11 Javascript
js jquery获取随机生成id的服务器控件的三种方法
2013/07/11 Javascript
常用的几段javascript代码分享
2014/03/25 Javascript
jquery常用特效方法使用示例
2014/04/25 Javascript
AngularJs解决跨域问题案例详解(简单方法)
2016/05/19 Javascript
javascript和jQuery实现网页实时聊天的ajax长轮询
2016/07/20 Javascript
Node.js包管理器Yarn的入门介绍与安装
2016/10/17 Javascript
js表单登陆验证示例
2016/10/19 Javascript
react开发中如何使用require.ensure加载es6风格的组件
2017/05/09 Javascript
React 使用browserHistory项目访问404问题解决
2018/06/01 Javascript
使用VUE+iView+.Net Core上传图片的方法示例
2019/01/04 Javascript
用vscode开发vue应用的方法步骤
2019/05/06 Javascript
Vue实现一种简单的无限循环滚动动画的示例
2021/01/10 Vue.js
[52:36]VGJ.S vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
Python完全识别验证码自动登录实例详解
2019/11/24 Python
Python应用实现双指数函数及拟合代码实例
2020/06/19 Python
django跳转页面传参的实现
2020/09/17 Python
递归实现回文判断(如:abcdedbca就是回文,判断一个面试者对递归理解的简单程序)
2013/04/28 面试题
文化宣传方案
2014/03/13 职场文书
教师廉洁自律承诺书
2014/05/26 职场文书
私营公司诉讼代理委托书范本
2014/09/13 职场文书
2014年图书管理员工作总结
2014/12/01 职场文书
2015年工会工作总结
2015/03/30 职场文书
2015年扶贫帮困工作总结
2015/05/20 职场文书
2016年毕业实习心得体会范文
2015/10/09 职场文书
在前女友婚礼上,用Python破解了现场的WIFI还把名称改成了
2021/05/28 Python