找不出哪裡出錯了,但是預覽時就是會出現一大堆亂碼
ob_clean();
header("Content-Type: image/png");
$img=@imagecreatetruecolor(50,20) or die("fjhb");
$bg_color=imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$bg_color);
$border_color=imagecolorallocate($img,200,200,200);
imagerectangle($img,0,0,49,19,$border_color);
for($i=2;$i<18;$i++){
$line_color=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
imageline($img,2,$i,47,$i,$line_color);
}
$font_size=12;
$str[0]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str[1]="abcdefghijklmnopqrstuvwxyz";
$str[2]="01234567891234567890123456";
$imgstr[0]["s"]=$str[rand(0,2)][rand(0,25)];
$imgstr[0]["x"]=rand(2,5);
$imgstr[0]["y"]=rand(1,4);
$imgstr[1]["s"]=$str[rand(0,2)][rand(0,25)];
$imgstr[1]["x"]=$imgstr[0]["x"]+$font_size-1+rand(0,1);
$imgstr[1]["y"]=rand(1,3);
$imgstr[2]["s"]=$str[rand(0,2)][rand(0,25)];
$imgstr[2]["x"]=$imgstr[1]["x"]+$font_size-1+rand(0,1);
$imgstr[2]["y"]=rand(1,4);
$imgstr[3]["s"]=$str[rand(0,2)][rand(0,25)];
$imgstr[3]["x"]=$imgstr[2]["x"]+$font_size-1+rand(0,1);
$imgstr[3]["y"]=rand(1,3);
for($i=0;$i<4;$i++){
$text_color=imagecolorallocate($img,rand(50,180),rand(50,180),rand(50,180));
imagechar($img,$font_size,$imgstr[$i]["x"],$imgstr[$i]["y"],$imgstr[$i]["s"],$text_color);
}
imagepng($img);
imagedestroy($img);
?>
回複討論(解決方案)
亂碼是瀏覽器顯示了字元而不是圖片嗎?
關閉瀏覽器重新訪問這個地址試試。如果還不行,應該是header的問題。
圖片輸出,最好用UTF-8字元集,因為有可能會遇到中文的輸出。
而UTF-8的話,就要注意BOM頭了。
樓主很幽默
ob_clean(); //清除緩衝區內容,這樣即便有BOM頭,也不會影響圖片
header("Content-Type: image/png"); //聲明其後內容是png圖片資料
於是即便程式有非文法性錯誤,至多也只是一個叉,而不是亂碼
我這裡測試正常哦
檢查一下有沒有去掉BOM頭
gd庫等環境是否完備
最主要的感覺還是header問題,header之前有別的輸出嗎?
是不是檔案的編碼原因?
lZ 是否預覽是如下的亂碼:
出現這樣的問題是因為檔案編碼為:
然後你需要修改成
就OK了,預覽效果:
昨晚不論我怎麼清理緩衝,關閉瀏覽器,改編碼,一直一堆亂碼,今天電腦啟動,再測試了網頁,居然莫名其妙恢複正常了,完全摸不著頭腦,只能罵破電腦,玩我那!!!!