PHP開發(fā)驗證碼教程之實現(xiàn)數(shù)字驗證碼
實現(xiàn)數(shù)字驗證碼
<?php $image = imagecreatetruecolor(100,30); $bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF imagefill($image,0,0,$bgcolor); for ($i=0;$i<4;$i++){ $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); $fontcontent = rand(0,9); $x = ($i * 100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } header('content-type: image/png'); imagepng($image); //銷毀 imagedestroy($image); ?>
for ($i=0;$i<4;$i++){ //循環(huán)出四個數(shù)字 $fontsize = 6;//字體的大小為6 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); //定義字體的顏色 $fontcontent = rand(0,9);//定義字體的取值范圍 }$x = ($i * 100/4)+rand(5,10); $y = rand(5,10); 在范圍內(nèi)隨機定義坐標 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); }
imagestring()?用?col?顏色將字符串?s?畫到?image?所代表的圖像的?x,y?坐標處(這是字符串左上角坐標,整幅圖像的左上角為 0,0)。如果font?是 1,2,3,4 或 5,則使用內(nèi)置字體。
注意:控制好字體的大小和分布,避免字體的重疊和顯示不全