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 中的输出缓冲
Dec 21 PHP
Windows IIS PHP 5.2 安装与配置方法
Jun 08 PHP
PHP手机号码归属地查询代码(API接口/mysql)
Sep 04 PHP
使用PHP下载CSS文件中的图片的代码
Sep 24 PHP
win7计划任务定时执行PHP脚本设置图解
May 09 PHP
thinkphp实现发送邮件密码找回功能实例
Dec 01 PHP
PHP实现批量生成App各种尺寸Logo
Mar 19 PHP
php实现修改新闻时删除图片的方法
May 12 PHP
全面了解PHP中的全局变量
Jun 17 PHP
一键生成各种尺寸Icon的php脚本(实例)
Feb 08 PHP
PHP递归实现文件夹的复制、删除、查看大小操作示例
Aug 11 PHP
PHP耦合设计模式实例分析
Aug 08 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实现面包屑导航(当前位置)例子分享
2014/05/10 PHP
php写入、删除与复制文件的方法
2015/06/20 PHP
浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势
2017/11/08 PHP
layui框架实现文件上传及TP3.2.3(thinkPHP)对上传文件进行后台处理操作示例
2018/05/12 PHP
随机显示经典句子或诗歌的javascript脚本
2007/08/04 Javascript
基于jQuery的计算文本框字数的代码
2012/06/06 Javascript
JS实现静止元素自动移动示例
2014/04/14 Javascript
node.js中的querystring.parse方法使用说明
2014/12/10 Javascript
Js使用WScript.Shell对象执行.bat文件和cmd命令
2014/12/18 Javascript
javascript文本框内输入文字倒计数的方法
2015/02/24 Javascript
JavaScript返回网页中超链接数量的方法
2015/04/03 Javascript
JQuery工具函数汇总
2015/06/15 Javascript
jQuery的框架介绍
2016/05/11 Javascript
Bootstrap模态框调用功能实现方法
2016/09/19 Javascript
微信小程序 点击控件后选中其它反选实例详解
2017/02/21 Javascript
微信小程序 参数传递实例代码
2017/03/20 Javascript
Vue2.0实现将页面中表格数据导出excel的实例
2017/08/09 Javascript
NodeJs form-data格式传输文件的方法
2017/12/13 NodeJs
JavaScript canvas实现雨滴特效
2021/01/10 Javascript
简单上手Python中装饰器的使用
2015/07/12 Python
将字典转换为DataFrame并进行频次统计的方法
2018/04/08 Python
解决Python2.7读写文件中的中文乱码问题
2018/04/12 Python
python找出完数的方法
2018/11/12 Python
TensorFLow 不同大小图片的TFrecords存取实例
2020/01/20 Python
经济实惠的豪华家具:My-Furniture
2019/03/12 全球购物
客服端调用EJB对象的几个基本步骤
2012/01/15 面试题
搞笑创意广告语
2014/03/17 职场文书
社会实践活动总结范文
2014/07/03 职场文书
学习焦裕禄精神践行三严三实心得体会
2014/10/13 职场文书
工作证明英文模板
2014/10/21 职场文书
逃课检讨书
2015/01/26 职场文书
2015年社区精神文明工作总结
2015/05/26 职场文书
交通安全教育心得体会
2016/01/15 职场文书
SQL Server表分区删除详情
2021/10/16 SQL Server
动画电影《龙珠超 超级英雄》延期上映
2022/03/20 日漫
CI Games宣布《堕落之王2》使用虚幻引擎5制作 预计将于2023年正式发售
2022/04/11 其他游戏