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);
通过这个基础练习,学习到了简单的处理问题的方法,满足目标的要求,不要盲目的敲代码。
比如这个验证码,其核心是imagettftext()函数,围绕这个函数来展开验证码的设计。
注意:range()函数的运用,参数如果是字符的话,要用单引号或双引号,否则header头会认为是有输出。
如对本文有疑问, 点击进行留言回复!!
(精华)2020年7月21日 ASP.NET Core 容器伪属性注入
网友评论