php ,驗證碼圖片,亂碼解決辦法

來源:互聯網
上載者:User
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了,預覽效果:
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.