?php
/**
* vcode
* param $w画布宽
* param $h 画布高
* param $fnum 验证码元素个数
* param $fontsize 验证码元素大小
* param $obs 干扰素个数
*/
function vcode($w, $h, $fnum, $fontsize, $obs) {
$img = imagecreatetruecolor($w, $h);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0 , 0, 0);
$gray = imagecolorallocate($img, 180, 180, 180);
imagefill($img, 0, 0, $gray);
for ($i=0; $i $obs; $i++) {
imagesetpixel($img, rand(0, $w), rand(0, $h), $black);
}
$x = ($w - $fnum * $fontsize) / 2;
$y = $h / 2 + $fontsize / 2;
$fontfile = "final/fonts/msyh.ttf";
$arr = array_merge(range(0,9), range('a', 'z'), range('A', 'Z'));
shuffle($arr);
$str=implode(array_slice($arr, 0, $fnum));
imagettftext($img, $fontsize, 0, $x, $y, $black, $fontfile, $str);
header("content-type:image/jpeg");
imagejpeg($img);
imagedestroy($img);
}
vcode(80, 40, 4, 15, 1000);