PHP開發(fā)驗證碼教程之加入內(nèi)容與干擾元素
下面我們來看一下加入內(nèi)容與干擾元素
<?php //創(chuàng)建一個畫布 $width = 85; $height = 30; $type = 2; $length = 4; $line = 5; //線的個數(shù) $pix = 50; //點的個數(shù) $img = imagecreatetruecolor($width, $height); //準(zhǔn)備填充的顏色 $white = imagecolorallocate($img,255,255,255); $black = imagecolorallocate($img,0,0,0); //向畫布填充圖形或者文字 imagefilledrectangle($img,1,1,$width-2,$height-2,$white); $chars = rangeString($type,$length); for($i=0;$i<$length;$i++){ $size = mt_rand(14,18); $x = 5+$size*$i; $y = mt_rand(8,$length*2); $text = substr($chars,$i,1); $color = imagecolorallocate($img,mt_rand(50,90),mt_rand(90,200),mt_rand(80,120)); imagestring($img,$size,$x,$y,$text,$color); } //障礙物像素點 for($i=0;$i<$pix;$i++){ $px = mt_rand(1,$width-2); $py = mt_rand(1,$height-2); $color = imagecolorallocate($img,mt_rand(50,90),mt_rand(90,200),mt_rand(80,120)); imagesetpixel($img, $px, $py, $color); } //障礙物橫線 for($i=0;$i<$line;$i++){ $color = imagecolorallocate($img,mt_rand(50,90),mt_rand(90,200),mt_rand(80,120)); imageline($img,mt_rand(1,$width-2), mt_rand(1,$height-2),mt_rand(1,$width-2), mt_rand(1,$height-2),$color); } //輸出圖像 header("content-type:image/png"); imagepng($img); //銷毀資源 imagedestroy($img); /** * 隨機字符串 * @param [type] $type int [1:數(shù)字;2:字母;3:數(shù)字加字母] * @param [type] $length int 4 * @return [type] string */ function rangeString($type,$length){ if($type==1){ $chars = join("",range(0,9)); }elseif($type==2){ $chars = join("",array_merge(range("a","z"),range("A","Z"))); }if($type==3){ $chars = join("",array_merge(range(0,9),range("a","z"),range("A","Z"))); } $chars = str_shuffle($chars); $chars = substr($chars,0,$length); return $chars; }
如上代碼 ,可以去改type 的值? $type=1 是數(shù)字
$type = 2?? 是大寫字母或者小寫字母,
$type = 3 是數(shù)字與字母的組合,
大家可以試一下