<?php
header("Content-type:image/png");
session_start();
$image = imagecreatetruecolor(120,50); //创建 100*50 px 的真彩色画布
$color = imagecolorallocate($image,220,220,220);
imagefill($image,0,0,$color); //填充画布
$blue = imagecolorallocate($image,0,0,255);
$font = 5; //字符串大小 1-5
//随机字符串
$arr1 = range("0","9");
$arr2 = range("A","Z");
$bigarr = array_merge($arr1,$arr2);
$rand = array_rand($bigarr,4);
$string = "";
foreach($rand as $v){ //遍历取出随机数组中的值,这个值大数组里的键
$string.=$bigarr[$v];
}
$_SESSION['verify']=$string;
$width = imagesx($image);
$height = imagesy($image);
$fontWidth = imagefontwidth($font)*strlen($string);
$fontHeight = imagefontheight($font);
$fontX = ($width-$fontWidth)/2;
$fontY = ($height-$fontHeight)/2;
imagestring($image,$font,$fontX,$fontY,$string,$blue);
//干扰线
for($i=0;$i<=2;$i++){
$lineColor = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($image,mt_rand(0,120),mt_rand(0,50),mt_rand(0,120),mt_rand(0,50),$lineColor);
}
imagepng($image); //输出图像
imagedestroy($image); //销毁图像
?>
封装一个验证码类
最新推荐文章于 2021-05-12 18:14:03 发布