利用PHP的GD库制作动态验证码是非常实用的方法。
PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType、Type1等字体库。
JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。
PNG 是可移植的网络图像,对图像采用了无损压缩标准。
GIF 原义是“图像互换格式”,是一种基于LZW算法的连续色调的无损压缩格式 。
imagecreate -- 新建一个基于调色板的图像
resource imagecreate ( int x_size, int y_size )
本函数用来建立空新画布,参数为图片大小,单位为像素 (pixel)。支持256色。
imagecreatetruecolor -- 新建一个真彩色图像
resource imagecreatetruecolor ( int x_size, int y_size )
新建一个真彩色图像画布 ,需要 GD 2.0.1 或更高版本,不能用于 GIF 文件格式。
小提示
使用 imagecreate 和 imagecreatetruecolor 函数都可以新建画布,主要区别是背景颜色填充上不同:
imagecreate 创建的画布用 imagecolorallocate 函数可以直接填充背景
imagecreatetruecolor 创建的画布用 imagefill 函数填充背景,不能直接用 imagecolorallocate 填充。
imagedestroy -- 销毁一图像
bool imagedestroy ( resource image )
imagedestroy() 释放与 image 关联的内存。
在输出或使用完图片资源后,用 imagedestroy 销毁图像资源,可以有效释放内存,这是个好习惯!
imagecolorallocate -- 为一幅图像分配颜色
语法:int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
imagegif -- 以 GIF 格式将图像输出到浏览器或文件
语法:bool imagegif (resource image [,string filename] )
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
语法:bool imagejpeg (resource image [,string filename [, int quality]] )
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
语法:bool imagepng (resource image [,string filename] )
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
语法:bool imagewbmp (resource image [, string filename [, int foreground]] )
imagefill -- 区域填充
语法:bool imagefill(resource image,int x,int y, int color)
imagesetpixel -- 画一个单一像素
语法:bool imagesetpixel ( resource image, int x, int y, int color )
imageline -- 画一条线段
语法:bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imagefilledrectangle -- 画一矩形并填充
语法:bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )
imagepolygon -- 画一个多边形
语法:bool imagepolygon ( resource image, array points, int num_points, int color )
imagefilledpolygon -- 画一多边形并填充
语法:bool imagefilledpolygon ( resource image, array points, int num_points, int color )
imageellipse -- 画一个椭圆
语法:bool imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse -- 画一椭圆并填充
语法:bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagearc -- 画椭圆弧
bool imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color )
imagefilledarc -- 画一椭圆弧且填充
bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagestring -- 水平地画一行字符串
语法:bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestringup -- 垂直地画一行字符串
语法:bool imagestringup ( resource image, int font, int x, int y, string s, int col )
imagechar -- 水平地画一个字符
语法:bool imagechar ( resource image, int font, int x, int y, string c, int color )
imagecharup -- 垂直地画一个字符
语法:bool imagecharup ( resource image, int font, int x, int y, string c, int color )
imagettftext -- 用 TrueType 字体向图像写入文本
语法 :array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
示例
$im = imagecreatetruecolor(300, 200);
//$im = imagecreate(300, 200);
$color = imagecolorallocate($im, 200,200,100);
$color1 = imagecolorallocate($im, 0,0,0);
$color2 = imagecolorallocate($im, 225,0,0);
$color3 = imagecolorallocate($im, 0,225,0);
$color4 = imagecolorallocate($im, 0,0,225);
imagefill($im, 0, 0, $color);
imagesetpixel($im, 50, 50, $color1);
imageline($im, 30,20,200,150, $color2);
imagefilledrectangle($im, 100,50,180,200, $color3);
imagefilledpolygon($im, array(150,100,180,50,250,150),3,$color4);
imagestring($im, 1, 20,100, 'hello world!', $color1);
imagettftext($im, 16, 45, 30,180,$color4,'fonts/1.ttf','Welcome to China!');
header("content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
验证码的绘制与使用
验证码生成文件 code.php
<?php
$im = imagecreatetruecolor(100,50);
$color_bg = imagecolorallocate($im, 225,225,225);
$color_text = imagecolorallocate($im, 255,0,0);
imagefill($im,0,0,$color_bg);
$str = 'asdfghjklzxcvbnwertyuRTYUUDFGHJCVB759465161656';
$len = strlen($str)-1;
$i = 1;
while($i < 5){
$number = rand(0,$len);
$text.= $str[$number];
$i++;
}
setcookie('code',$text); //设置cookie值用来验证
imagettftext($im,16,0,10,33,$color_text,'fonts/1.ttf',$text);
header("content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
表单呈现页面 login.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
</head>
<body>
<form method="post">
用户名:<input type="text" name="name"/><br><br>
密码:<input type="text" name="password"/><br><br>
验证码:<input type="text" name="code"/><img width="100" height="50" id="code" src="code.php" onclick="yz()"/><br><br>
<input type="submit" value="提交"/>
</form>
<script>
function yz(){
var pic = document.getElementById('code');
pic.src = "code.php?"+Math.random(); //生成随机的数字,可以获取刷新验证码图片的效果
}
</script>
<?php
if($_POST['code']){
if($_POST['code'] == $_COOKIE['code']){
echo "<script> alert('验证码正确!');</script>";
}else{
echo "<script> alert('验证码不正确!');</script>";
}
}
?>
</body>
</html>
