PHP封装验证码

<?php
// 对验证码进行封装
function cone() {
	$width = 100;
	$height = 50;
	// 生成验证码的画布
	$image = imagecreatetruecolor($width, $height);
	$gray = imagecolorallocate($image, 200, 200, 200);
	$red = mt_rand(1, 199);
	$green = mt_rand(1, 199);
	$blue = mt_rand(1, 199);
	// 使用rgb来获取产生字体的字体颜色
	$color = imagecolorallocate($image, $red, $green, $blue);
	imagefill($image, 0, 0, $gray);
	// 0-9 a-z 的随机数
	$string = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
	for ($i = 0; $i < 4; $i++) {
		$size = mt_rand(20, 30);
		$angle = mt_rand(-15, 15);
		$x = 100 / 4 * $i;
		$by = (50 - $size) / 2 + $size;
		$y = mt_rand($by * 0.8, $by * 1.2);
		$fontfile = __DIR__ . '/GiddyupStd.otf';  //可自己更换字体
		$info = mt_rand(0, strlen($string) - 1);
		$text = $string[$info];
		imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
	}
	// 为了防止机器人操作添加干扰
	for ($i = 0; $i < 10; $i++) {
		$x1 = mt_rand(0, 100);
		$y1 = mt_rand(0, 50);
		$x2 = mt_rand(0, 100);
		$y2 = mt_rand(0, 50);
		imageline($image, $x1, $y1, $x2, $y2, $color);
	}
	// 转化图片的生成的形式
	header('content-type:image/png');
	imagepng($image);
	imagedestroy($image);
}
cone();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值