為什么加上了$type = 1;$length = 4;之后 瀏覽網頁 是一張破損的圖片呢??之前沒寫的時候和老師的結果是一樣的呀~~

qq_慕婉清4370237
2014-09-28
4 回答
舉報
0/150
提交
取消
2014-09-28
$type=3,join那是等號, 出錯了。
其他的沒看出來問題,你在看看,是不是有錯誤信息輸出,在header之前有任何輸出都不能顯示圖片
你看看行不行,不行我們在討論
^-^...
2014-09-28
代碼發下 我看看^-^...
2014-09-28
<?php
require_once 'string.func.php';
//通過GD庫做驗證碼
//創建畫布
$width = 80;
$height = 30;
$image = imagecreatetruecolor($width,$height);
//用白色顏色填充畫布
$white = imagecolorallocate($image,255,255,255);
//黑色畫筆
$black = imagecolorallocate($image,0,0,0);
//用填充矩形填充畫布
imagefilledrectangle($image,1,1,$width-2,$height-2,$white);
//生成隨機字符串
$type = 1;
$length = 4;
$chars = buildRandomString($type,$length);
//設置session名字
$sess_name = "verify";
//將生成的隨機字符串放于session中 便于與用戶輸入比對
$_SESSION[$sess_name] = $chars;
//字體
$fontfiles = array("a_d_mono.ttf","abaddon.TTF","MM.TTF");
for($i = 0;$i<$length;$i++){
?? ?//產生一個隨機的的字體大小
?? ?$size = mt_rand(14,18);
?? ?//隨機的角度
?? ?$angle = mt_rand(-15,15);
?? ?//產生隨機的橫坐標
?? ?$x = 5+$i*$size;
?? ?//產生隨機的縱坐標
?? ?$y = mt_rand(20,26);
?? ?//產生隨機的顏色
?? ?$color = imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
?? ?//隨機字體
?? ?$fontfile = "../fonts/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
?? ?$text = substr($chars,$i,1);
?? ?imagettftext($image,$size,$angle,$x,$y);
?? ?
}
?
header ( "content-type:image/gif" );
imagegif($image);
imagedestroy($imgage);
?>
<?php
function buildRandomString($type=1,$length=4){
?? ?if($type == 1){
?? ??? ?$chars = join("",range(0,9));
?? ? }elseif ($type == 2){
?? ??? ?$chars = join("",array_merge(range("a","z"),range("A","Z")));
?? ? }elseif($type ==3 ){
?? ??? ?$chars - join("",array_merge(range(0,9),range("a","z"),range("A","Z")));
?? ? }
?? ?if($length > strlen($chars)){
?? ??? ?exit("字符串長度不夠");
?? ?}
?? ?$chars = str_shuffle($chars);
?? ?return substr($chars,0,$length);
}
?>
2014-09-28