一漂亮的PHP图片验证码实例


Posted in PHP onMarch 21, 2014

一、显示效果
一漂亮的PHP图片验证码实例
二、代码如下

/*
 *  @Author fy
 */
$imgwidth =100; //图片宽度
$imgheight =40; //图片高度
$codelen =4; //验证码长度
$fontsize =20; //字体大小
$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
$font = 'Fonts/segoesc.ttf';
$im=imagecreatetruecolor($imgwidth,$imgheight);
$while=imageColorAllocate($im,255,255,255);
imagefill($im,0,0,$while); //填充图像
//取得字符串
$authstr='';
$_len = strlen($charset)-1;
for ($i=0;$i<$codelen;$i++) {
 $authstr .= $charset[mt_rand(0,$_len)];
}
session_start();
$_SESSION['scode']=strtolower($authstr);//全部转为小写,主要是为了不区分大小写
//随机画点,已经改为划星星了
for ($i=0;$i<$imgwidth;$i++){
    $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
 imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
    //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
}
//随机画线,线条数量=字符数量(随便)
for($i=0;$i<$codelen;$i++) 
{  
 $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
 imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor); 
} 
$_x=intval($imgwidth/$codelen); //计算字符距离
$_y=intval($imgheight*0.7); //字符显示在图片70%的位置
for($i=0;$i<strlen($authstr);$i++){
 $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
 //imagestring($im,5,$j,5,$imgstr[$i],$color3);
 // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
 imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);
}
//生成图像
header("content-type:image/PNG");
imagePNG($im);
imageDestroy($im);

 

PHP 相关文章推荐
将数字格式的计算结果转为汉字格式
Oct 09 PHP
php 全文搜索和替换的实现代码
Jul 29 PHP
网页游戏开发入门教程二(游戏模式+系统)
Nov 02 PHP
PHP中simplexml_load_string函数使用说明
Jan 01 PHP
Zend的MVC机制使用分析(一)
May 02 PHP
解析php中heredoc的使用方法
Jun 17 PHP
ThinkPHP自定义函数解决模板标签加减运算的方法
Jul 03 PHP
PHP基本语法实例总结
Sep 09 PHP
php实现的SSO单点登录系统接入功能示例分析
Oct 12 PHP
详解yii2实现分库分表的方案与思路
Feb 03 PHP
php正确输出json数据的实例讲解
Aug 21 PHP
ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
Apr 02 PHP
PHP中nowdoc和heredoc使用需要注意的一点
Mar 21 #PHP
php获取表单中多个同名input元素的值
Mar 20 #PHP
php中的静态变量的基本用法
Mar 20 #PHP
php遍历文件夹下的所有文件和子文件夹示例
Mar 20 #PHP
php数据库备份还原类分享
Mar 20 #PHP
PHP实现微信公众平台音乐点播
Mar 20 #PHP
一个PHP针对数字的加密解密类
Mar 20 #PHP
You might like
基于qmail的完整WEBMAIL解决方案安装详解
2006/10/09 PHP
PHP静态类
2006/11/25 PHP
第4章 数据处理-php正则表达式-郑阿奇(续)
2011/07/04 PHP
定义php常量的详解
2013/06/09 PHP
php使用glob函数快速查询指定目录文件的方法
2014/11/15 PHP
php对数组内元素进行随机调换的方法
2015/05/12 PHP
js给onclick赋值传参数的两种方法
2013/11/25 Javascript
javascript实现的字符串与十六进制表示字符串相互转换方法
2015/07/17 Javascript
scroll事件实现监控滚动条并分页显示(zepto.js)
2016/12/18 Javascript
jquery ajaxfileupload异步上传插件使用详解
2017/02/08 Javascript
解析Vue2.0双向绑定实现原理
2017/02/23 Javascript
利用Vue v-model实现一个自定义的表单组件
2017/04/27 Javascript
基于jQuery实现Ajax验证用户名是否可用实例
2018/03/25 jQuery
总结JavaScript在IE9之前版本中内存泄露问题
2018/04/28 Javascript
vue通过滚动行为实现从列表到详情,返回列表原位置的方法
2018/08/31 Javascript
bootstrap datepicker的基本使用教程
2019/07/09 Javascript
[01:13]这,就是刀塔
2014/07/16 DOTA
[47:10]完美世界DOTA2联赛PWL S3 LBZS vs Rebirth 第二场 12.16
2020/12/18 DOTA
python解决汉字编码问题:Unicode Decode Error
2017/01/19 Python
python+opencv实现动态物体识别
2018/01/09 Python
python微信好友数据分析详解
2018/11/19 Python
python 设置输出图像的像素大小方法
2019/07/04 Python
Python搭建Spark分布式集群环境
2019/07/05 Python
世界首屈一指的钓鱼用品商店:TackleDirect
2016/07/26 全球购物
Harrods英国:世界领先的奢侈品百货商店
2020/09/23 全球购物
Wiggle新西兰:自行车、跑步、游泳
2020/05/06 全球购物
PHP数据运算类型都有哪些
2013/11/05 面试题
高中毕业生自我鉴定
2013/11/03 职场文书
播音主持专业个人自我评价
2014/01/09 职场文书
《小熊住山洞》教学反思
2014/02/21 职场文书
大学生个人自荐信
2014/02/24 职场文书
暑假安全保证书
2015/02/28 职场文书
女儿满月酒致辞
2015/07/29 职场文书
病假条格式范文
2015/08/17 职场文书
redis连接被拒绝的解决方案
2021/04/12 Redis
《王国之心》迎来了发售的20周年, 野村哲发布贺图
2022/04/11 其他游戏