PHP验证码生成
<?php
//后台生成验证码
function vcode($number=4,$size=20,$width=0,$height=0,$str="0123456789",$font=array('function/calibri.ttf')){
if($width==0)
{
$width=($number+1)*$size;
}
if($height==0)
{
$height=$size*2.5;
}
$im=imagecreatetruecolor($width,$height);
$randTintColor=imagecolorallocate($im,rand(160,255),rand(160,255),rand(160,255));
$randColor=imagecolorallocate($im,rand(0,80),rand(0,80),rand(0,80));
imagefilledrectangle($im, 0, 0, $width, $height, $randTintColor);
$vcode='';
$fontMaxIndex=count($font)-1;
for ($i=0; $i <$number ; $i++) {
$wx=$size*0.5+$size*$i;
$wy=rand($size*1.5,$size*2);
$strMaxIndex=strlen($str)-1;
$code=$str[rand(0,$strMaxIndex)];
$vcode.=$code;
$randColor1=imagecolorallocate($im,rand(0,120),rand(0,120),rand(0,120));
imagettftext($im, $size , rand(-10,10), $wx, $wy, $randColor1, $font[rand(0,$fontMaxIndex)], $code);
}
$pn=$size*5;
for ($i=0; $i < $pn; $i++) {
$randColor=imagecolorallocate($im,rand(0,80),rand(0,80),rand(0,80));
$wwx= rand(0,$width);
$wwy= rand(0,$height);
imagesetpixel($im, $wwx, $wwy, $randColor);
}
if(isset($_SESSION)){
session_start();
}
$_SESSION['vcode']=$vcode;
header("Content-type: image/png;charset=utf-8");
ob_clean();
imagepng($im);
imagedestroy($im);
}
?>