原生PHP生成验证码

session_start();

//指定以图片形式响应
header("Content-type: image/png");

//产生验证码,并将其放在session中; 
$randval="";
for($i=0;$i<4;$i++){
   $randval.=" ".mt_rand(0,9);
}
$_SESSION['randcode'] = $randval;

//背景颜色
$rb = $_REQUEST['rb'] ?: 225;
$gb = $_REQUEST['gb'] ?: 241;
$bb = $_REQUEST['bb'] ?: 255;

//文字颜色
$rt = $_REQUEST['rt'] ?: 10;
$gt = $_REQUEST['gt'] ?: 160;
$bt = $_REQUEST['bt'] ?: 243;

$width= 80;   //二维码图片宽度
$height= 25;  //二维码图片高度
$fontcolor =  Array(1,1,255);
$im = @imagecreate($width,$height);
$stringColor = ImageColorAllocate($im,$rt,$gt,$bt); //字体颜色
$backColor = ImageColorAllocate($im,$rb,$gb,$bb);//背景色
// $borderColor = ImageColorAllocate($im, 18,121,226);//边框色
// $pointColor = ImageColorAllocate($im, 16,67,118);//点颜色

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
@imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置

for($i=0;$i<=150;$i++){
	$pointX = rand(2,$width-2);
	$pointY = rand(2,$height-2);
   @imagesetpixel($im, $pointX, $pointY, $pointColor);//绘模糊作用的点
}

@imagestring($im, 5, 0, 5, $randval, $stringColor); //调整字型位置
$ImageFun='Imagepng';
$ImageFun($im);
@ImageDestroy($im);

 

你可能感兴趣的:(PHP)