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

PHP基础------GD库绘制验证码

2015-09-19 11:10 597 查看
<?php
一:GD函数库

1.函数:
imagecreate(width,height) 创建了基于调色板的图像

imagecreatetruecolor(width,height) 创建基于真色彩的图像

header("content-type:image/图片格式");
图片格式:gif jpeg png

imagegif(image)输出图像

imagedestroy(image)销毁图像

imagestring(image, font, x, y, string, color)写入字符串

imagecolorallocate(image, red, green, blue)为图像分配颜色

imagettftext(
image, //图像
size, //文本大小 单位px
angle, //倾斜的角度
x, //文字起点的坐标
y, //文字起点的坐标
color, //颜色
fontfile, //ttf文件的位置
text//文本内容
)

imagefilledrectangle(image, x1, y1, x2, y2, color)绘制一个矩形

imagesetpixel(image, x, y, color)绘制单独的像素点

imageline(image, x1, y1, x2, y2, color)绘制一个线段
?>


验证码案例:

<?php
header("content-type:image/gif");
//验证码宽高
$width = "150";
$height = "60";

//创建画布
$image = imagecreatetruecolor($width, $height);

//分配颜色
$color = imagecolorallocate($image, 255, 255, 255);

//文字颜色
$sizeColor = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));

//填充画布
imagefilledrectangle($image, 0, 0, 150, 60, $color);

//验证码文本
$num = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$checkStr = null;

//验证码的位数
for ($i=0; $i < 4; $i++) {
$checkStr = $checkStr.$num[rand(0,35)];
}

//写入文本
imagettftext($image, 25, mt_rand(-5,5), 20, 30, $sizeColor, "segoeprb.ttf", $checkStr);

//绘制噪点
for ($i=0; $i <100 ; $i++) {
imagesetpixel($image, mt_rand(0,150), mt_rand(0,60),$sizeColor);
}

//绘制干扰线
for ($i=0; $i < 4; $i++) {
imageline($image,mt_rand(0,150), mt_rand(0,60), mt_rand(0,150), mt_rand(0,60), $sizeColor);
}

//输出图像
imagegif($image);
//销毁图像
imagedestroy($image);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: