php 创建验证码方法
2016-01-26 16:59
519 查看
php创建验证码方法:
<?php function getVerify($length=4,$sessName='verify'){ //验证码 //获取字符串 去除01ol等较难辨认字符 $chars = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; $chars = str_shuffle($chars); $chars = substr($chars, 0, $length); //将字符存入session $_SESSION[$sessName] = $chars; //定义画布大小 $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); //定义背景颜色和边框颜色 $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white); //定义验证码颜色 $verifyColor = imagecolorallocate($image, 15, 164, 80); //将字符写入画布 for($i=0; $i<$length; $i++){ $size = 18; $angle = mt_rand(-12, 12); $x = 5 + $i*$size; $y = 23; $fontfile = 'GEORGIA.TTF';//字体 $color = $verifyColor; $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //干扰点 for($i=0; $i<80; $i++){ imagesetpixel($image, mt_rand(0, $width-1), mt_rand(0, $height-1), $verifyColor); } //干扰线 for($i=0; $i<3; $i++){ imageline($image, mt_rand(0, $width-1), mt_rand(0, $height-1), mt_rand(0, $width-1), mt_rand(0, $height-1), $verifyColor); } //告诉浏览器输出格式 header("content-type:image/gif"); //输出 imagegif($image); //销毁,释放内存 imagedestroy($image); }
相关文章推荐
- 关于PHP 的strtotime("2016.01.26")不输出问题bug查找
- H3C S3600-28TP-SI配置命令
- 与《YII框架》不得不说的故事—5篇目录
- 关于phpcms中mysql和mysqli的区别
- PHP实现日志写入log.txt
- Yii 用户登录体
- 一键生成各种尺寸Icon的php脚本
- FragmentPagerAdapter API
- ThinkPHP单字母函数(快捷方法)使用总结
- 数据库--PHP环境搭建
- 【Symfony2官方文档翻译】 phpunit
- 任意类型转换为IntPtr
- EditPlus快捷键
- php常用加密函数总结
- php代码收集
- PHP数组游标实现对数组的各种操作详解
- PHP面向对象程序设计实例分析
- PHP——连接数据库初
- PHP接收post过来的xml数据
- CI框架 -- 核心文件 之 Output.php(输出类文件)