批改状态:合格
老师批语:封装到类中比较好, 可以再壮阔你的类, 完成验证码验证
小项目

验证码session使用
public static function create(){// 配置$config = self::_getCodeConfig();//创建画布$image = imagecreatetruecolor($config['width'],$config['height']);//背景颜色$bgcolor=imagecolorallocate($image,255,255,255);imagefill($image,0,0,$bgcolor);$captch_code = '';//存储验证码$captchCodeArr = str_split($config['codeStr']);//随机选取4个候选字符for($i=0;$i<$config['codeCount'];$i++){$fontsize = $config['fontsize'];$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色$fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];$captch_code.=$fontcontent;$_x = $config['width']/$config['codeCount'];$x=($i*(int)$_x)+rand(5,10); //随机坐标$y=rand(5,10);imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串//imagefttext($im, $size, 2, $size * (0.5 + $i * 1.1), $size * 1.2, $font, Env::get('root_path') . 'public/static/fonts/COOPBL.TTF', $authnum);}session_start();$_SESSION['code']=$captch_code;//增加干扰点if($config['disturbPoint']){for($i=0;$i<$config['pointCount'];$i++){$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);}}//增加干扰线if($config['disturbLine']){for($i=0;$i<$config['lineCount'];$i++){$linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);}}//输出格式header('Content-Type:image/png');imagepng($image);//销毁图片imagedestroy($image);}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号