php生成验证码并显示在浏览器

php文件名是image.func.php

<?php
        session_start();
	// //创建画布
	$width=80;
	$height=28;
	$image=imagecreatetruecolor($width, $height);//返回以画像,默认黑色
	
	$white=imagecolorallocate($image,255,255,255);//为画布上色
	$black=imagecolorallocate($image,0,0,0);//为画布上色
	// //用填充矩形填充画布
	//imagefilledrectangle()意思是在$image画布上画了一个$white颜色的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。并无返回数据
	imagefilledrectangle($image, 0, 0, $width, $height, $white);
	
	$chars=buildRandomString($type,$length);
	
	$_SESSION[$sess_name]=$chars;
	$fontfiles=array("msyh.ttc","msyhbd.ttc","msyhl.ttc","simsun.ttc");

	for ($i=0; $i <$length ; $i++) { 
		//mt_rand()生成x到y范围内随机数
		$size=mt_rand(14,18);
		$angle=mt_rand(-15,15);
		$x=5+$i*$size;
		$y=mt_rand(20,26);
		$color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
		$fontfile="../fonts/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
		$text=substr($chars,$i,1);
		//向图像写入文本
		imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
	}
	
	if ($pixel) {
		for ($i=0; $i < 50; $i++) { 
			//imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
			imagesetpixel($image,mt_rand(0,$width-1),mt_rand(0,$height-1),$black);
		}
	}

	
	if ($line) {
		for ($i=1; $i < $line; $i++) { 
			$color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
			//imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
			imageline($image,mt_rand(0,$width-1),mt_rand(0,$height-1),mt_rand(0,$width-1),mt_rand(0,$height-1),$color);
		}
	}
	header("content-type:image/png");
	//imagegif — 输出图象到浏览器或文件。
	// imagegif($image);
	imagepng($image);
	imagedestroy($image);
?>


html代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<img src="http://localhost/PHPDemo/lib/image.func.php" οnclick="getVerify()" id="verify">
	<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
	<script type="text/javascript">

		function getVerify() {
			$("#verify").attr("src","http://localhost/PHPDemo/lib/image.func.php");
		}
		
	</script>
</body>
</html>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值