php ,驗證碼圖片,亂碼
找不出哪裡出錯了,但是預覽時就是會出現一大堆亂碼
ob_clean();
header("Content-Type: image/png");
[email protected](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);
?> PHP 圖片 驗證碼
------解決方案--------------------
亂碼是瀏覽器顯示了字元而不是圖片嗎?
關閉瀏覽器重新訪問這個地址試試。如果還不行,應該是header的問題。
圖片輸出,最好用UTF-8字元集,因為有可能會遇到中文的輸出。
而UTF-8的話,就要注意BOM頭了。
------解決方案--------------------
我這裡測試正常哦
檢查一下有沒有去掉BOM頭
gd庫等環境是否完備
最主要的感覺還是header問題,header之前有別的輸出嗎?
------解決方案--------------------
lZ 是否預覽是如下的亂碼:
出現這樣的問題是因為檔案編碼為:
然後你需要修改成
就OK了,預覽效果: