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 相关文章推荐
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
PHP 多维数组排序(usort,uasort)
Jun 30 PHP
PHP数组 为文章加关键字连接 文章内容自动加链接
Dec 29 PHP
php多用户读写文件冲突的解决办法
Nov 06 PHP
用PHP代替JS玩转DOM的思路及示例代码
Jun 15 PHP
Yii 快速,安全,专业的PHP框架
Sep 03 PHP
高质量PHP代码的50个实用技巧必备(下)
Jan 22 PHP
CakePHP框架Model关联对象用法分析
Aug 04 PHP
PHP编程中的Session阻塞问题与解决方法分析
Aug 07 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
Feb 11 PHP
PHP使用PDO实现mysql防注入功能详解
Dec 20 PHP
Thinkphp集成抖音SDK的实现方法
Apr 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
浅析echo(),print(),print_r(),return之间的区别
2013/11/27 PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
2014/09/04 PHP
PHP概率计算函数汇总
2015/09/13 PHP
PHP封装的分页类与简单用法示例
2019/02/25 PHP
在laravel5.2中实现点击用户头像更改头像的方法
2019/10/14 PHP
jQuery判断iframe中元素是否存在的方法
2013/05/11 Javascript
js、jquery图片动画、动态切换示例代码
2014/06/03 Javascript
JS中实现简单Formatter函数示例代码
2014/08/19 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
js图片轮播特效代码分享
2015/09/07 Javascript
深入分析jQuery的ready函数是如何工作的(工作原理)
2015/12/17 Javascript
使用jQuery判断Div是否在可视区域的方法 判断div是否可见
2016/02/17 Javascript
JS实现控制文本框的内容
2016/07/10 Javascript
Bootstrap组件系列之福利篇几款好用的组件(推荐二)
2016/07/12 Javascript
微信公众号开发 自定义菜单跳转页面并获取用户信息实例详解
2016/12/08 Javascript
Vue数据驱动模拟实现1
2017/01/11 Javascript
详解从angular-cli:1.0.0-beta.28.3升级到@angular/cli:1.0.0
2017/05/22 Javascript
Vue使用vue-draggable 插件在不同列表之间拖拽功能
2020/03/12 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
Python实现的石头剪子布代码分享
2014/08/22 Python
python实现挑选出来100以内的质数
2015/03/24 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
2018/01/16 Python
python实现图像识别功能
2018/01/29 Python
在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南
2018/01/30 Python
对python pandas读取剪贴板内容的方法详解
2019/01/24 Python
深入浅析python变量加逗号,的含义
2020/02/22 Python
Python如何在main中调用函数内的函数方式
2020/06/01 Python
opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
2020/06/02 Python
CSS3中线性颜色渐变的一些实现方法
2015/07/14 HTML / CSS
HTML5的Video标签有部分MP4无法播放的问题解析(多图)
2017/08/18 HTML / CSS
ALDO美国官网:加拿大女鞋品牌
2018/12/28 全球购物
英国最大的在线蜡烛商店:Candles Direct
2019/03/26 全球购物
策划主管的工作职责
2013/11/24 职场文书
日语专业毕业生自荐书
2014/06/18 职场文书
六一儿童节致辞稿(3篇)
2019/07/11 职场文书
人事部:年度述职报告范文
2019/07/12 职场文书