一、验证码
做验证码之前先看一看要用到的函数。
mt_rand() 生成随机数
mt_rand — 生成更好的随机数 int mt_rand ( int $min , int $max ) 返回 min (或者 0) 到 max (或者是到 mt_getrandmax() ,包含这个值)之间的随机整数。
dechex() 将十进制数字转换成十六进制
dechex — 十进制转换为十六进制 string dechex ( int $number ) 返回一字符串,包含有给定 number 参数的十六进制表示。
imagecreatetruecolor() 创建画布
imagecreatetruecolor — 新建一个真彩色图像 resource imagecreatetruecolor ( int $width , int $height ) imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
imagecolorallocate() 创建画笔
imagecolorallocate — 为一幅图像分配颜色 int imagecolorallocate ( resource $image , int $red , int $green , int $blue ) imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。 red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数
imagefill() 填充图像
imagefill — 区域填充 bool imagefill ( resource $image , int $x , int $y , int $color ) imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充 (即与 x, y 点颜色相同且相邻的点都会被填充)。
imagerectangle() 画一个矩形
imagerectangle — 画一个矩形 bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) imagerectangle() 用 col 颜色在 image 图像中画一个矩形, 其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。
imageline() 画一条线段
imageline — 画一条线段 bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imagestring() 画一行字符串
imagestring — 水平地画一行字符串 bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col ) imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处 (这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
ob_clean()
ob_clean — 清空(擦掉)输出缓冲区 void ob_clean ( void ) 此函数用来丢弃输出缓冲区中的内容。
imagepng() 输出图像
imagepng — 以 PNG 格式将图像输出到浏览器或文件 bool imagepng ( resource $image [, string $filename ] ) imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器), 或者如果用 filename 给出了文件名则将其输出到该文件。
imagedestroy() 销毁图像
imagedestroy — 销毁一图像 bool imagedestroy ( resource $image ) imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,
代码: 没做成函数
<?php //【1】得到随机验证码,并放到session中 session_start(); //随机码的个数 $_rand_str = 4; for($_i=0;$_i<$_rand_str;$_i++){ $_nmsg .= dechex(mt_rand(0, 15)); } //现在就得到了验证码,但是这个验证码在注册界面是看不到的, //所以我们要把这个随机的验证码保存在服务器上,保存在一个可以跨页面、持久的东西上。 $_SESSION['code'] = $_nmsg; //【2】创建一张图 $_width = 75; $_height = 25; $_img = imagecreatetruecolor($_width, $_height); //【3】填充背景 //创建颜色 $_white = imagecolorallocate($_img, 255, 255, 255); //填充 imagefill($_img, 0, 0, $_white); //【4】创建黑色的边框--有没有边框做成可控的 $_flog = FALSE; if($_flog){ //创建颜色 $_black = imagecolorallocate($_img, 0, 0, 0); //画一个矩形 imagerectangle($_img, 0, 0, $_width - 1, $_height - 1, $_black); } //【5】画出干扰 //[5.1]随机画出6个线条 for($_i=0;$_i<6;$_i++){ //创建一个随机色 $_rand_color = imagecolorallocate($_img,mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //画出线条 imageline($_img, mt_rand(0, $_width), mt_rand(0, $_height), mt_rand(0, $_width), mt_rand(0, $_height), $_rand_color); } //[5.2]随机雪花 for($_i=0;$_i<50;$_i++){ //创建随机颜色-200~255 颜色比较淡 $_rand_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //画出 * 字符 imagestring($_img, 1, mt_rand(0, $_width),mt_rand(0, $_height), "*", $_rand_color); } //【6】输出验证码 -- 注意此处的位置算法 for($_i=0;$_i<strlen($_SESSION['code']);$_i++){ //创建颜色 $_color = imagecolorallocate($_img, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //画验证码 imagestring($_img, mt_rand(4,5) , $_i*$_width/$_rand_str+mt_rand(1, 10) , mt_rand(1, $_height/2), $_SESSION['code'][$_i], $_color); } //输出图像 ob_clean(); //不写高版本可能会出错 //[1]声明格式 header("Content-type:image/png"); //[2]输出 imagepng($_img); //销毁掉 imagedestroy($_img); ?>
二、验证码局部刷新
在code的<img >中加入如下代码就能实现