标签:
<?php
/*
图片验证码 Powered By KASON test http://www.111cn.net */
session_start();
//getCode(4,84,37);
code_verify(84,37,4);
/**
* 大写,小写,数字混合
*/
function alpnum_verify() {
$length = 4;
$code = "";
$alpnum_arr = array();
for($i = 97; $i <= 122; $i++) {
$alpnum_arr[] = chr($i);
}
for($i = 65; $i <= 90; $i++) {
$alpnum_arr[] = chr($i);
}
for($i = 0; $i < 10; $i++) {
$alpnum_arr[] = $i;
}
$unset_arr = array(‘0‘,‘o‘,‘O‘,‘I‘,‘i‘,‘l‘);
foreach($unset_arr as $v) {
$key = array_search($v,$alpnum_arr);
unset($alpnum_arr[$key]);
}
shuffle($alpnum_arr); //打乱数组
for($i = 0; $i < $length; $i++) {
$alpha_site = rand(0,55);
$code .= $alpnum_arr[$alpha_site];
}
return $code;
}
/******************************
*$num = 4;//验证码个数
*$width = 80;//验证码宽度
* $height = 20;//验证码高度
******************************/
function getCode($num,$width,$height) {
$code=‘ ‘;
for($i=0;$i<$num;$i++)//生成验证码
{
switch(rand(0,2))
{
case 0:$code[$i]=chr(rand(48,57));break;//数字
case 1:$code[$i]=chr(rand(65,90));break;//大写字母
case 2:$code[$i]=chr(rand(97,122));break;//小写字母
}
}
$_SESSION["VerifyCode"] = $code;
$image=imagecreate($width,$height);
//随机绘制两条虚线,起干扰作用
$y1 = rand(0, $height);
$y2 = rand(0, $height);
$y3 = rand(0, $height);
$y4 = rand(0, $height);
imageline($image, 0, $y1, $width, $y3, IMG_COLOR_STYLED);
imageline($image, 0, $y2, $width, $y4, IMG_COLOR_STYLED);
imagecolorallocate($image,255,255,255);
for($i=0;$i<80;$i++)//生成干扰像素
{
$dis_color = imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
}
for($i=0; $i<$num; $i++)//打印字符到图像
{
$char_color = imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagechar($image,600,($width/$num)*$i,rand(0,15),$code[$i],$char_color);
}
header("Content-type:image/png");
imagepng($image);//输出图像到浏览器
imagedestroy($image);//释放资源
}
/**
*
*/
function code_verify($w,$h,$num) {
$code = "";
$code = alpnum_verify();
$_SESSION["VerifyCode"] = $code;
//创建图片,定义颜色值
header("Content-type: image/PNG");
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, 0, 0, 0);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
$border_color = imagecolorallocate($im, rand(0,255),rand(0,255),rand(0,255));
//填充背景
imagefill($im, 0, 0, $bgcolor);
//画边框
imagerectangle($im, 0, 0, $w-1, $h-1, $border_color);
//随机绘制两条虚线,起干扰作用
for($i = 2; $i < 4; $i++) {
$noise_color = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,$w/2),rand(0,$h/2),rand($w/2,$w),rand($h/2,$h),$noise_color);
}
$font = dirname(__FILE__)."/../font/font.ttf";
//在画布上随机生成大量黑点,起干扰作用;
for ($i = 0; $i < 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx = 5;
$TFontAngle = array(-20,20); //旋转角度
$anglelen = count($TFontAngle)-1;
for ($i = 0; $i < $num; $i++) {
$strpos = rand(20, 25);
$angle = $TFontAngle[rand(0,$anglelen)];
imagettftext($im, rand(17,20), $angle, $strx, $strpos,$black , $font, $code[$i]);
$strx += rand(10, 20);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
?>
html:
<img src="action/VerifyCode.php" class="yzm-img" title="看不清?换一个"/>
标签:
原文地址:http://www.cnblogs.com/yll-sww/p/4446654.html