您的位置:首页 > 编程语言 > PHP开发

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: