黑色背景中間白框,沒有顯示驗證碼,弄了一天了,求解救
<?php /** ?*?Created?by?PhpStorm. ?*?User:?a117 ?*?Date:?2018/10/14 ?*?Time:?5:45?PM ?*/ 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); //比對驗證碼 $sess_name="verify"; $_SESSION[$sess_name]=$chars; //字體數組 $fontfile=array("Avenir.ttc","Helvetica.ttc","Kohinoor.ttc"); //產生驗證碼 for?($i=9;$i<$length;$i++){ ????$size=mt_rand(14,18); ????$angle=mt_rand(-15,15); ????$x=5+$i*$size; ????$y=mt_rand(20,26); ????$fontfile="../fonts/".$fontfile[mt_rand(0,count($fontfile)-1)]; ????$color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180)); ????//截取驗證碼 ????$text=substr($chars,$i,1); ????imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text); } header("content-type:image/gif"); imagegif($image); imagedestroy($image);
下面是string.func.php的
<?php /** ?*?Created?by?PhpStorm. ?*?User:?a117 ?*?Date:?2018/10/14 ?*?Time:?5:46?PM ?*/ 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("a",?"z"),?range("A",?"Z"),?range(0,9))); ????} ????if?($length?>?strlen($chars))?{ ????????exit("字符串長度不夠"); ????} ????$chars?=?str_shuffle($chars); //截取驗證碼長度 ????return?substr($chars,?0,?$length); }
GD庫應該開了
2022-03-24
把例子的里網頁鏈接改為本地的路徑即可我發現我用的是html5的開頭部分,就是圖中顯示的這樣,換成html其他版本就正常顯示了,不知道這是為什么?
2018-12-23
我也出現了這種情況,如果你的代碼跟老師的一模一樣且運行結果沒有出現驗證碼,試著切換到5.*版本運行
2018-11-15
黑背景可能是瀏覽器的原因,好象沒關系的,不妨礙以后代碼的使用。您使用360??
2018-11-14
2018-11-14
2018-10-24
我也是