帮下小虾米

WBOY
Release: 2016-06-13 13:29:04
Original
911 people have browsed it

大虾帮下小虾米。
//使用session_start()
session_start();


//创建随机数
for($i=0;$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 $_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 $_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);
?>


为什么出来的是乱码???(还有输出验证码环节未写完。)

------解决方案--------------------
要想显示图片程序就只能输出图片内容,绝对不能有其它输出,包括BOM头,报错等等。
你 echo $_SESSION['code']; 扰乱了图片的输出
------解决方案--------------------

PHP 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);
?>
<br><font color="#e78608">------解决方案--------------------</font><br>把最后的环节改一下,然后看看有什么错误信息,以及test.png是否正常<br>
Copy after login
PHP code
//保存图像,不输出
// header("content-Type:image/png");
imagepng($_img, 'test.png');
//销毁函数
imagedestroy($_img); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template