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中static和const关键字的区别
Mar 19 PHP
php addslashes 函数详细分析说明
Jun 23 PHP
php feof用来识别文件末尾字符的方法
Aug 01 PHP
PHP+MYSQL会员系统的登陆即权限判断实现代码
Sep 23 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
May 15 PHP
PHP可变函数的使用详解
Jun 14 PHP
php生成html文件方法总结
Dec 01 PHP
使用GDB调试PHP代码,解决PHP代码死循环问题
Mar 02 PHP
php简单实现文件或图片强制下载的方法
Dec 06 PHP
PHP时间处理类操作示例
Sep 05 PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
Aug 20 PHP
php使用Swoole实现毫秒级定时任务的方法
Sep 04 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
文章推荐系统(三)
2006/10/09 PHP
用户的详细注册和判断
2006/10/09 PHP
php中3种方法删除字符串中间的空格
2014/03/10 PHP
兼容各大浏览器带关闭按钮的漂浮多组图片广告代码
2014/06/05 PHP
JavaScript高级程序设计(第3版)学习笔记6 初识js对象
2012/10/11 Javascript
改变文件域的样式实现思路同时兼容ie、firefox
2013/10/23 Javascript
让JavaScript的Alert弹出框失效的方法禁止弹出警告框
2014/09/03 Javascript
jquery+ajax实现跨域请求的方法
2015/01/20 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
JQuery和HTML5 Canvas实现弹幕效果
2017/01/04 Javascript
js实现导航栏中英文切换效果
2017/01/16 Javascript
详解利用jsx写vue组件的方法示例
2017/07/17 Javascript
javascript cookie的基本操作(添加和删除)
2017/07/24 Javascript
微信小程序报错:this.setData is not a function的解决办法
2017/09/27 Javascript
基于Vue实现拖拽效果
2018/04/27 Javascript
微信公众平台 客服接口发消息的实现代码(Java接口开发)
2019/04/17 Javascript
vue.js实现备忘录demo
2019/06/26 Javascript
vue自定义表单生成器form-create使用详解
2019/07/19 Javascript
layui输入框中只允许输入整数的实现方法
2019/09/18 Javascript
vue实现防抖的实例代码
2021/01/11 Vue.js
python中list列表的高级函数
2016/05/17 Python
python3获取当前文件的上一级目录实例
2018/04/26 Python
python安装virtualenv虚拟环境步骤图文详解
2019/09/18 Python
python实现三种随机请求头方式
2021/01/05 Python
jupyter 添加不同内核的操作
2021/02/06 Python
CSS3模块的目前的状况分析
2010/02/24 HTML / CSS
CSS3 滤镜 webkit-filter详细介绍及使用方法
2012/12/27 HTML / CSS
html5利用canvas实现颜色容差抠图功能
2019/12/23 HTML / CSS
马来西亚网上购物平台:ezbuy
2018/02/13 全球购物
药学专业大学生个人的自我评价
2013/11/04 职场文书
美术国培研修感言
2014/02/12 职场文书
遗嘱继承公证书
2014/04/09 职场文书
汽车技术服务与贸易专业求职信
2014/07/20 职场文书
大学生职业生涯十年规划书范文
2014/09/17 职场文书
2015年司机年终工作总结
2015/05/14 职场文书
html中两种获取标签内的值的方法
2022/06/16 jQuery