php 验证码程序
代码如下 | 复制代码 |
session_start(); $te1 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im,rand()%100,rand()%30,$te1); |
/*
生成验证的作用是防止用户乱注册了,这是一等的验证程序
*/
# 显示图形及连接
function showdir ($adirectory, $i)
{
global $browsedir;
$start = $i;
# 更改 $maxcols 及 $maximages 可改变每一页显示的小图的行数与列数。
$maxcols = 2;
$maximages = 6;
$maximages = $i + ($maximages - 3);
# 更改 $imagemaxwidth 及 $imagemaxheight 可改变显示小图的宽度与高度。
$imagemaxwidth = 100;
$imagemaxheight = 100;
# 计算高度与宽度的比例。
$imagemaxratio = $imagemaxwidth / $imagemaxheight;
$ndirectory = sizeof ($adirectory);
echo (table_start);
for ($i; $i<=$maximages;)
{
echo (row_start);
for ($icols=1; $icols<=$maxcols; $icols++)
{
echo (col_start);
$imagefilename = $adirectory[++$i];
if (strlen($imagefilename)>0)
{
$imagepath = $browsedir."/".$imagefilename;
$imagesize = getimagesize ($imagepath);
if ($imagesize)
{
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$imageratio = $imagewidth / $imageheight;
if ($imageratio > $imagemaxratio)
{
$imageoutputwidth = $imagemaxwidth;
$imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
}
else if ($imageratio < $imagemaxratio)
{
$imageoutputheight = $imagemaxheight;
$imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
} else
{
$imageoutputwidth = $imagemaxwidth;
$imageoutputheight = $imagemaxheight;
}
# 显示图形
echo (a_start.$imagepath.a_close);
echo (img_start.$imagepath.img_width.$imageoutputwidth.img_height.$imageoutputheight.img_end);
echo (line_break.$adirectory[$i]);
echo (a_end);
}
echo (col_end);
}
}
echo (row_end);
}
echo (table_end);
pagemenu ($browsedir, $ndirectory, $start);
}
function pagemenu ($browsedir, $ndirectory, $pg) {
echo "<br><center><font face="verdana, arial, helvetica, sans-serif" size="1" color="#000033">page:";
$pg_num = 1;
for ($img_num = 0; $img_num <= $ndirectory;) {
if ($pg == $img_num) {
echo "<span class="menulink_1"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> *$pg_num</a> <span>";
} else {
echo "<span class="menulink_2"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> $pg_num</a> <span>";
}
# 建立其他页次的连接, 每页显示四张图, 故页数 $pg_num 每加 1 , $img_num 就加 4 。
$pg_num = $pg_num + 1;
$img_num = $img_num + 4;
}
echo "</font> ";
}
function dirtoarray ($browsedir, $extensions)
{
$nextensions = sizeof ($extensions);
$idirectory = 0;
$directory = dir($browsedir);
while ($entry = $directory->read())
{
for ($i=1; $i<=$nextensions; $i++)
{
$compare = stristr ($entry, $extensions[$i]);
if (strlen($compare) == strlen($extensions[$i]))
{
$adirectory[++$idirectory] = $entry;
break;
}
}
}
$directory->close();
return $adirectory;
}
#主程序
#变量 $browsedir 为图形文件放置的位置。
$browsedir="./images";
# 允许浏览的图形文件扩展名, 放置於数组中, 可自行增加。
$extensions[1] = "jpeg";
$extensions[2] = "jpg";
$extensions[3] = "gif";
$extensions[4] = "png";
showdir (dirtoarray ($browsedir, $extensions), $start);
define ("line_break", "<br>");
define ("table_start", "<table width=600> ");
define ("table_end", "</table> ");
define ("row_start", " <tr> ");
define ("row_end", " </tr> ");
define ("col_start", " <td align=center> ");
define ("col_end", " </td> ");
define ("img_start", "<img src=");
define ("img_end", ">");
define ("img_width", " width=");
define ("img_height", " height=");
define ("a_start", '<a href="');
define ("a_close", '">');
define ("a_end", "</a>");
?>
1 生成一个简单图像。
2 设定图像的颜色。
3 在图像上绘制直线。
4 在图像上显示文字。
5 在图像中显示中文字符。
6 打开已存在的图片。
7 获取图片的相关属性。
8 函数getimagesize()的用法。
9 为上传图片添加水印效果。
10 生成已有图片的缩略图。
11 使用函数imagecopyresampled()。
12 生成带有底纹的数字验证码图片的php程序。
*/
//1 生成一个简单图像。
代码如下 | 复制代码 |
$width = 200; $img = imagecreatetruecolor($width,$height) or die("不支持gd图像处理"); |
//2 设定图像的颜色。
代码如下 | 复制代码 |
$width = 200; $img = imagecreatetruecolor($width,$height) or die("不支持gd图像处理"); $bg_color = imagecolorallocate($img, 255, 0, 0); imagepng($img); |
//3 在图像上绘制直线。
代码如下 | 复制代码 |
$width = 200; $img = imagecreatetruecolor($width,$height) or die("不支持gd图像处理"); $line_color = imagecolorallocate($img, 255, 255, 255); imagepng($img); |
//4 在图像上显示文字。
代码如下 | 复制代码 |
$width = 200; $img = imagecreatetruecolor($width,$height) or die("不支持gd图像处理"); imageline($img, 0, 40, 200, 40, $line_color); imagepng($img); |
//5 在图像中显示中文字符。
代码如下 | 复制代码 |
$width = 200; $img = imagecreatetruecolor($width,$height) or die("不支持gd图像处理"); //“西游记”3个字16进制字符 //“吴承恩著”4个字16进制字符 imageline($img, 0, 40, 200, 40, $line_color); //竖排显示“西游记”3字 //横排显示“吴承恩著”4字 imagepng($img); //6 打开已存在的图片。 imagejpeg($img); //7 获取图片的相关属性。 $x = imagesx($img); //8 函数getimagesize()的用法。 for($i=0; $i<4; ++$i) |
?>
这是一款利用php自带的功能把指定的大图生成我们指定大小的缩略图代码哦,使用方便简单,只要把设置下面四个参数就可以生成自己想的大小的缩略图哦。
代码如下 | 复制代码 |
function bigtosmallimg($file,$path,$w=120,$h=90) switch($stype){//根据上传好的图形文件类型新建一个用来生成缩略图的源文件。 $desf =imagecreatetruecolor($mw,$mh); imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh); } |
//此缩略图调用方法
代码如下 | 复制代码 |
bigtosmallimg($file,$path,$w=120,$h=90); /* |
$file = 图片的路径
$path = 生成后保存的路径
$w =图片宽度
$h =图片高度
*/
有3个文件:
authcode.php-----验证码的生成php文件
authcode.html-----前台显示页面
dealauthcode.php-----ajax提交到的后台处理判断验证码是否正确的处理页面
*/
?>
前台调用验证码代码
代码如下 | 复制代码 |
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> |
dealauthcode.php-----ajax提交到的后台处理判断验证码是否正确的处理页面
代码如下 | 复制代码 |
<?php session_start(); $authcode = $_get['authcode']; //这里的$_session['authcode']是在验证码authcode页面产生的 if(strtoupper($authcode)!= $_session['authcode']){ echo 'mis_match'; } ?> 上一篇: 加入干扰的生成验证码代码 下一篇: php 读取目录下图像文件
相关文章
|