這篇文章主要介紹了PHP驗證碼無法顯示的原因及解決辦法的相關資料,這裡說明了如何出現無法顯示的原因及相應的解決辦法,需要的朋友可以參考下
PHP驗證碼無法顯示的原因及解決辦法
一、如果是utf-8,就有可能是BOM沒有清除
二、在Header("Content-type: image/PNG"); 之前有輸出
三、第一行PHP隱藏了代碼,如空格,斷行符號等。 解決代碼:
$image_width=70; //設定映像寬度$image_height=18; //設定映像高度$new_number=$_GET[num];//$new_number=5;$num_image=imagecreate($image_width,$image_height); //建立一個畫布imagecolorallocate($num_image,255,255,255); //設定畫布的顏色$black=imagecolorallocate($num_image,0,0,0);/**/for($i=0;$i<strlen($new_number);$i++){ //迴圈讀取SESSION變數中的驗證碼 $font=mt_rand(3,5); //設定隨機的字型 $x=mt_rand(1,8)+$image_width*$i/4; //設定隨機字元所在位置的X座標 $y=mt_rand(1,$image_height/4); //設定隨機字元所在位置的Y座標 $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //設定字元的顏色 imagestring($num_image,$font,$x,$y,$new_number[$i],$color); //水平輸出字元}header("content-type:image/png"); //設定建立映像的格式imagepng($num_image); //產生PNG格式的映像imagedestroy($num_image); //釋放映像資源