<?php
//使用session_start()
session_start();
//创建随机数
for($i=0;$i<4;$i++){
$_nmsg .= dechex(mt_rand(1,15));
}
//保存在全局变量session中
$_SESSION['code'] = $_nmsg;
echo $_SESSION['code'];
//长和高
$_width=75;
$_height=25;
//创建一张图片
$_img = imagecreatetruecolor($_width,$_height);
//创建颜色
$_rn_color = imagecolorallocate($_img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //填充颜色
imagefill($_img,0,0,$_rn_color);
/* //创建黑色边框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
*/
//随机画出6个线条
for($i=0;$i<6;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//随机打雪花
for($i=0;$i<100;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring ($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
//输出验证码
//输出图像
header("content-Type:image/png");
imagepng($_img);
//销毁函数
imagedestroy($_img);
?>
为什么出来的是乱码???(还有输出验证码环节未写完。)
//使用session_start()
session_start();
//创建随机数
for($i=0;$i<4;$i++){
$_nmsg .= dechex(mt_rand(1,15));
}
//保存在全局变量session中
$_SESSION['code'] = $_nmsg;
echo $_SESSION['code'];
//长和高
$_width=75;
$_height=25;
//创建一张图片
$_img = imagecreatetruecolor($_width,$_height);
//创建颜色
$_rn_color = imagecolorallocate($_img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //填充颜色
imagefill($_img,0,0,$_rn_color);
/* //创建黑色边框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
*/
//随机画出6个线条
for($i=0;$i<6;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//随机打雪花
for($i=0;$i<100;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring ($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}
//输出验证码
//输出图像
header("content-Type:image/png");
imagepng($_img);
//销毁函数
imagedestroy($_img);
?>
为什么出来的是乱码???(还有输出验证码环节未写完。)
你 echo $_SESSION['code']; 扰乱了图片的输出
//创建随机数
for($i=0;$i<4;$i++){
$_nmsg .= dechex(mt_rand(1,15));
} //保存在全局变量session中
$_SESSION['code'] = $_nmsg; echo $_SESSION['code'];
删掉之后就显示出来了。很是纠结啊
<?php
//使用session_start()
session_start();$_nmsg = '';//创建随机数
for($i=0;$i<4;$i++){
$_nmsg .= dechex(mt_rand(1,15));
}//保存在全局变量session中
$_SESSION['code'] = $_nmsg;//echo $_SESSION['code'];//长和高
$_width=75;
$_height=25;//创建一张图片
$_img = imagecreatetruecolor($_width,$_height);//创建颜色
//$_rn_color = imagecolorallocate($_img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
$_rn_color = imagecolorallocate($_img, 255,255,255);//填充颜色
imagefill($_img,0,0,$_rn_color);/* //创建黑色边框
$_black = imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
*/
//随机画出6个线条
for($i=0;$i<6;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}//随机打雪花
for($i=0;$i<100;$i++){
$_rnd_color = imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring ($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),'*',$_rnd_color);
}//输出验证码
//$font = 'arial';
$text_color = imagecolorallocate($_img,255,0,0);
imagestring($_img,5,20,5,$_nmsg,$text_color);
//imagettftext($_img,10,0,10,20,$font,$_nmsg);//输出图像
header("content-Type:image/png");
imagepng($_img);//销毁函数
imagedestroy($_img);
?>
//保存图像,不输出
// header("content-Type:image/png");
imagepng($_img, 'test.png');
//销毁函数
imagedestroy($_img);