PHP 生成验证码

<?php 
/**
 * php 生成验证码
 * yzm.php
 * @author flyer0126
 */

//定义图像的宽、高
$width = 75;
$height = 25;

//创建一个真彩色图像
$img = imagecreatetruecolor($width, $height);
//为一幅图像分配颜色
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);

//画一个黑色矩形边框
$black = imagecolorallocate($img, 0, 0, 0);
imagerectangle($img, 0, 0, $width-1, $height-1, $black);

//随机画直线、打点、创建验证码
$msg = '';
for ($i = 0; $i < 4; $i++){
	$randcolor = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $randcolor);
	imagestring($img, 1, mt_rand(1, $width), mt_rand(1, $height), ".", $randcolor);
    $msg .= dechex(mt_rand(0,15));
}

//将验证码放到图片上
for ($j = 0; $j < strlen($msg); $j++){
	$randcolor = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	imagestring($img, mt_rand(3, 5), $j*$width/strlen($msg) + mt_rand(1, 10), mt_rand(1, $height/2), $msg[$j], $randcolor);
}

//以JPEG格式输出图像
header("Content-Type:image/jpeg");
imagejpeg($img);

//销毁图像【释放关联内存】
imagedestroy($img);
?>
 
<!-- 实现页面点击切换效果 -->
<img src="yzm.php" title="click change" οnclick="javascript:this.src='yzm.php?t='+Math.random();">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值