[php]代码库
<?php
//验证码的制作
//1.生成4位的随机数
for ($i = 0; $i < 4; $i++) {
$rand .= dechex(rand(1, 15));
}
//2.创建图片
//创建一张图片
$image = imagecreatetruecolor(100, 30);
//背景颜色
$bg = imagecolorallocate($image, 0, 0, 0);
$te = imagecolorallocate($image, 255, 255, 255);
//划线
for ($i = 0; $i < 4; $i++) {
$lineColor = imagecolorallocate($image, rand(20, 225), rand(20, 225), rand(20, 225));
imageline($image, rand(0, 100), 0, 100, 30, $lineColor);
}
//划点
for ($i = 0; $i < 200; $i++) {
imagesetpixel($image, rand() % 100, rand() % 100, $lineColor);
}
//添加文字 编码转换
$str = iconv("GBK", "UTF-8", "惊呆了");
imagettftext($image, 12, 11, 20, 20, $lineColor, 'simhei.ttf', $str);
//3.随机数写入图片$font=(1~6)
imagestring($image, rand(3, 6), rand(3, 70), rand(0, 16), $rand, $te);
//4.把验证码放入session中 用于后续验证
session_start();
$_SESSION[vilidationCode] = $rand;
header("Content-type: image/jpeg"); //输出图片
imagejpeg($image);
?>