<?php
// 宽度
$width = 80;
// 高度
$height = 38;
$imCode = imagecreatetruecolor($width,$height); // 创建真彩布
$bgColor = imagecolorallocate($imCode, 225, 200, 200);// 设置彩布的颜色值
imagefill($imCode, 0, 0, $bgColor); // 填充到彩布中去
// 绘制像素点
for ($i=0; $i < 100; $i++) { // 作用是进行干扰
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imagesetpixel($imCode, rand(0,$width), rand(0,$height), $color);
}
// 绘制直线 作用是进行干扰
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imageline($imCode, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $color);
}
// 获取随机验证码(四位数)
$str = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$code = '';
for ($i=0; $i < 4; $i++) {
$code .= substr($str, rand(0,strlen($str) - 1),1);
}
// 绘制验证码,文字
for ($i=0; $i < 4; $i++) {
// 颜色
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imagettftext($imCode, 18, rand(30,-30), 10+(18*$i), rand(20,30), $color, 'msyh.ttf', $code[$i]);
// imagettftext(图像资源,字号, 倾斜角度,X,Y, 颜色,字体文件名,文本);
}
// 输出图像
header('content-type:image/png');
// 输出在浏览器
imagepng($imCode);
// 释放资源
imagedestroy($imCode);
?>
// 宽度
$width = 80;
// 高度
$height = 38;
$imCode = imagecreatetruecolor($width,$height); // 创建真彩布
$bgColor = imagecolorallocate($imCode, 225, 200, 200);// 设置彩布的颜色值
imagefill($imCode, 0, 0, $bgColor); // 填充到彩布中去
// 绘制像素点
for ($i=0; $i < 100; $i++) { // 作用是进行干扰
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imagesetpixel($imCode, rand(0,$width), rand(0,$height), $color);
}
// 绘制直线 作用是进行干扰
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imageline($imCode, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $color);
}
// 获取随机验证码(四位数)
$str = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$code = '';
for ($i=0; $i < 4; $i++) {
$code .= substr($str, rand(0,strlen($str) - 1),1);
}
// 绘制验证码,文字
for ($i=0; $i < 4; $i++) {
// 颜色
$color = imagecolorallocate($imCode, round(0,255), round(0,255), round(0,255));
imagettftext($imCode, 18, rand(30,-30), 10+(18*$i), rand(20,30), $color, 'msyh.ttf', $code[$i]);
// imagettftext(图像资源,字号, 倾斜角度,X,Y, 颜色,字体文件名,文本);
}
// 输出图像
header('content-type:image/png');
// 输出在浏览器
imagepng($imCode);
// 释放资源
imagedestroy($imCode);
?>