<?php$string=md5(rand(1,1000));
$string=substr($string,0,4);
$len=strlen($string);
$bordercolor = "#000000";
$bgcolor = "#ffffff";//背景色
$height = 30;
$width = 60;$image = imageCreate(65, $height);
//画边框
$bordercolor = getcolor($image,$bordercolor);
imagefilledrectangle($image,0,0,66,$height+1,$bordercolor);
//画背景
$back = getcolor($image,$bgcolor);
imagefilledrectangle($image,1,1,65-2,$height-2,$back);
//画干扰元素
$num = 200; //干扰元素的个数
setnoise($image,$width,$height,$num);
//字体大小
$size = ceil($width / $len);
//写字for($i=0;$i < $len;$i++)
{
$TempText=substr($string,$i,1);
//字体颜色
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
//取得随机大小
$randsize =rand($size-$size/6,$size+$size/6);
//取得字体
$font = rand(1,1).".ttf";
//取得角度
$randAngle = rand(-15,15);
//取得每次的位置
$x=8+($width-$width/8)/$len*$i;
//echo "<br>";
//取得每次的高度
$y=rand($height-3,$height-10);
imagettftext($image,$randsize,$randAngle,$x,$y,$textColor,"verdanab.TTF",$TempText);
}
session_start();
$_SESSION["auth_code"] = $string;
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);//取得色彩
function getcolor($image,$color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
//画干扰点
function setnoise($image,$width,$height,$noisenum)
{
for ($i=0; $i<$noisenum; $i++){
//分配颜色
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
//画点
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
?>这段代码用于产生验证码图片,在php4下运行正常,升级php5后,只产生干扰点,而验证码却产生不了,大大们帮忙看下哪里有问题,请务必指出具体位置,我是菜鸟,帮帮忙,谢谢了。
$string=substr($string,0,4);
$len=strlen($string);
$bordercolor = "#000000";
$bgcolor = "#ffffff";//背景色
$height = 30;
$width = 60;$image = imageCreate(65, $height);
//画边框
$bordercolor = getcolor($image,$bordercolor);
imagefilledrectangle($image,0,0,66,$height+1,$bordercolor);
//画背景
$back = getcolor($image,$bgcolor);
imagefilledrectangle($image,1,1,65-2,$height-2,$back);
//画干扰元素
$num = 200; //干扰元素的个数
setnoise($image,$width,$height,$num);
//字体大小
$size = ceil($width / $len);
//写字for($i=0;$i < $len;$i++)
{
$TempText=substr($string,$i,1);
//字体颜色
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
//取得随机大小
$randsize =rand($size-$size/6,$size+$size/6);
//取得字体
$font = rand(1,1).".ttf";
//取得角度
$randAngle = rand(-15,15);
//取得每次的位置
$x=8+($width-$width/8)/$len*$i;
//echo "<br>";
//取得每次的高度
$y=rand($height-3,$height-10);
imagettftext($image,$randsize,$randAngle,$x,$y,$textColor,"verdanab.TTF",$TempText);
}
session_start();
$_SESSION["auth_code"] = $string;
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);//取得色彩
function getcolor($image,$color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
//画干扰点
function setnoise($image,$width,$height,$noisenum)
{
for ($i=0; $i<$noisenum; $i++){
//分配颜色
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
//画点
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
?>这段代码用于产生验证码图片,在php4下运行正常,升级php5后,只产生干扰点,而验证码却产生不了,大大们帮忙看下哪里有问题,请务必指出具体位置,我是菜鸟,帮帮忙,谢谢了。
现在看到的图像就全是花花绿绿的干扰点,但是没验证码。
display_errors = On;
error_reporting = E_ALL;
代码肯定是没问题的,我在php5.2上调了
Imagettftext($image,$randsize,$randAngle,$x,$y,$textColor,"impact.ttf",$TempText);上面第一行能够正常运行,
第二行就不行了,是Imagettftext函数问题。但是Imagettftext肯定是支持的,ttf文件我也放在目录下了。还是不能显示,高手帮忙啊。