原生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);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值