{
}
function HsvToRgb($H, $S, $V)
{
}
$size = 20;
$width = 80;
$height = 25;
// 产生4个随机字符
$randStr =array(rand(0, 9), rand(0, 9), rand(0, 9), rand(0, 9));
// 生成数字旋转角度
$degrees = array(rand(0, 45), rand(0, 45), rand(0, 45), rand(0, 45));
for($i = 0; $i < 4; ++$i)
{
}
$image = imagecreatetruecolor($size, $size);
$validate = imagecreatetruecolor($width, $height);
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
// 数字颜色
for($i = 0; $i < 4; ++$i)
{
}
imagefilledrectangle($validate, 0, 0, $width - 1, $height - 1, $back);
imagefilledrectangle($image, 0, 0, $size, $size, $back);
imagestring($image, 5, 6, 2, $randStr[0], $textcolor[0]);
$image = imagerotate($image, $degrees[0], $back);
imagecopy($validate, $image, 1, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10);
$image = imagecreatetruecolor($size, $size);
imagefilledrectangle($image, 0, 0, $size, $size, $back);
imagestring($image, 5, 6, 2, $randStr[1], $textcolor[1]);
$image = imagerotate($image, $degrees[1], $back);
imagecopy($validate, $image, 21, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10);
$image = imagecreatetruecolor($size, $size);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagestring($image, 5, 6, 2, $randStr[2], $textcolor[2]);
$image = imagerotate($image, $degrees[2], $back);
imagecopy($validate, $image, 41, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10);
$image = imagecreatetruecolor($size, $size);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagestring($image, 5, 6, 2, $randStr[3], $textcolor[3]);
$image = imagerotate($image, $degrees[3], $back);
imagecopy($validate, $image, 61, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10);
imagerectangle($validate, 0, 0, $width - 1, $height - 1, $border);
for($i = 0; $i < 200; ++$i) //加入干扰象素
{
}
// 干扰线使用颜色较明亮的颜色
$temp = RgbToHsv(rand(0, 255), rand(0, 255), rand(0, 255));
if($temp[2] < 200)
$temp = HsvToRgb($temp[0], $temp[1], $temp[2]);
$randlinecolor = imagecolorallocate($image, $temp[0], $temp[1], $temp[2]);
// 画5条干扰线
for ($i = 0;$i < 5; $i ++)
header('Content-type: image/png');
imagepng($validate);
imagedestroy($validate);
最后讨论一下新浪博客中的“收听验证码”,呵呵,看起来好像很复杂,考虑了一下,其实要实现也挺简单的,将单个数字录成mp3格式的声音文件,使用windows media player根据产生的数字播放出来(网页中嵌入个object)。考虑兼容问题最好使用flash播放。
提示一下:程序中有Rgb到Hsv颜色互换函数。