验证码效果图

本文介绍了验证码图像的生成过程,包括常用的字体库选择,并展示了一种验证码的效果图。通过代码实现,详细阐述了如何创建此类验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码中常用的字体库自己可以去网上下载,在我资源文件是下图类型。

效果图:


代码:

<?php
   $code = scode();
   echo $code;
   function scode($type = 'big') {
        $all_size_config = array(
            'big' => array(
                'img_w' => 190,
                'img_h' => 40,
                'font_size' => 30,
                'angle_l' => -25,
                'angle_r' => 25,
                'pixel_num' => 40,
                'pixel_color' => 8,
                'noise_font_size' => 5,
                'Yamplitude' => 17,
            ),
            'small' => array(
                'img_w' => 120,
                'img_h' => 25,
                'font_size' => 19,
                'angle_l' => -15,
                'angle_r' => 15,
                'pixel_num' => 25,
                'pixel_color' => 8,
                'noise_font_size' => 3,
                'Yamplitude' => 14,
            ),
            'tiny' => array(
                'img_w' => 58,
                'img_h' => 20,
                'font_size' => 13,
                'angle_l' => -10,
                'angle_r' => 10,
                'pixel_num' => 8,
                'pixel_color' => 8,
                'noise_font_size' => 3,
                'Yamplitude' => 7,
            )
        );

        if (!array_key_exists($type, $all_size_config)) {
            $type = 'big';
        }
        $config = $all_size_config[$type];

        $scale = 5;    //图片放大比例
        $img_w = $config['img_w'];  // 设置图片宽
        $img_h = $config['img_h']; // 设置图片高
        $font_size = $config['font_size'];   // 字体大小
        $angle_l = $config['angle_l'];  // 左偏角
        $angle_r = $config['angle_r'];   // 右偏角
        $code_str = "123456789abcdefghijklmnpqrstuvwxyz";
        $word_len = 4;    // 验证码位数
        $pixel_num = $config['pixel_num'];    // 杂点数目基数
        $pixel_color = $config['pixel_color'];    // 杂点只有 $pixel_color 种颜色  总的杂点数为$pixel_num*$pixel_color
        $noise_font_size = $config['noise_font_size']; // 杂点字体大小
        $session_key = "scode"; //自定义session键名

        $font_dir = $_SERVER ["DOCUMENT_ROOT"] . "/jx3/newblog/fonts/"; // 字体库
        $fonts_cfg = array(
//            'Antykwa' => array('spacing' => -4, 'font' => 'AntykwaBold.ttf'),
//            'Duality' => array('spacing' => -3, 'font' => 'Duality.ttf'),
            'New' => array('spacing' => -3, 'font' => 'new.ttf'),
        );

        session_start();

        //生成随机字符串
        $word = "";
        $code_str_len = strlen($code_str) - 1;
        for ($i = 0; $i < $word_len; $i++) {
            $word .= $code_str[rand(0, $code_str_len)];
        }

        $_SESSION [$session_key] = strtolower($word);

        //创建画布
        $image = imagecreatetruecolor($img_w * $scale, $img_h * $scale);
        imagefilledrectangle($image, 0, 0, $img_w * $scale, $img_h * $scale, imagecolorallocate($image, 255, 255, 255));

        $font = $fonts_cfg['New'];
        $font_path = $font_dir . $font['font'];
        $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
        $base_point_x = $img_w / 5 * $scale;
        $base_point_y = round(($img_h * $scale + (imagefontheight($font_size)) * $scale) / 2);
        //绘制文字
        for ($i = 0; $i < $word_len; ++$i) {
            $coords = imagettftext($image, $font_size * $scale, 0, $base_point_x, $base_point_y, $color, $font_path, mb_substr($word, $i, 1, 'utf-8'));
            $base_point_x = $coords[2] + $font['spacing'];
        }

        $Yperiod = rand(16, 19); //扭曲文字时需要使用的 y方向周期.
        $Yamplitude = $config['Yamplitude']; //振幅
        $rand_yperiod = $scale * $Yperiod;  //随机Y方向周期.
        $k = rand(0, 100); //正弦线初相位
        for ($i = 0; $i < ($img_h * $scale); $i++) {
            imagecopy($image, $image, sin($k + $i / $rand_yperiod) * ($scale * $Yamplitude), $i - 1, 0, $i, $img_w * $scale, 1);
        }

        //画干扰线.
        $A = mt_rand(1, $img_h * $scale / 2);                  // 振幅
        $b = mt_rand(-$img_h * $scale / 4, $img_h * $scale / 4);    // Y轴方向偏移量
        $f = mt_rand(-$img_w * $scale / 4, $img_w * $scale / 4);     // X轴方向偏移量
        $T = mt_rand($img_w * $scale * 0.5, $img_w * $scale);    // 周期
        $w = (2 * M_PI) / $T;

        $px1 = mt_rand($img_w * $scale * 0.1, $img_w * $scale * 0.3);  // 曲线横坐标起始位置
        $px2 = mt_rand($img_w * $scale * 0.6, $img_w * $scale * 0.9);  // 曲线横坐标结束位置
        $line_height = rand(9, 11);
        for ($px = $px1; $px <= $px2; $px = $px + 0.9) {
            if ($w != 0) {
                $py = $A * sin($w * $px + $f) + $b + $img_h / 2;
                imageline($image, $px + $line_height, $py, $px + $line_height, $py + $line_height, $color);
            }
        }

        //将验证码图片缩放至正常尺寸.
        $imResampled = imagecreatetruecolor($img_w, $img_h);
        imagecopyresampled($imResampled, $image, 0, 0, 0, 0, $img_w, $img_h, $img_w * $scale, $img_h * $scale
        );
        imagedestroy($image);
        $image = $imResampled;

        //    绘制杂点
        for ($i = 0; $i < $pixel_color; $i++) {
            $noise_color = imagecolorallocate($image, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
            for ($j = 0; $j < $pixel_num; $j++) {
                imagestring($image, $noise_font_size, mt_rand(-10, $img_w * $scale), mt_rand(-10, $img_h * $scale), $code_str[mt_rand(0, $code_str_len)], $noise_color);
            }
        }

        header("Cache-Control: no-cache, must-revalidate");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Pragma: no-cache");
        header("Cache-control: private");
        header('Content-Type: image/png');
        imagepng($image);
        imagedestroy($image);
        exit;
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值