温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP碎码——ci的验证码

发布时间:2020-07-19 18:48:54 来源:网络 阅读:395 作者:lc994811089 栏目:web开发


自己写验证码的时候在文字定位上拿捏不准


于是把ci验证码主要部分扣了出来,看看,一般般,干扰线是亮点


代码去除了很多判断,简化了很多,显示效果一样

<?php
$defaults = array(
    'word'        => '',
    'img_path'    => './',
    'img_url'    => '',
    'img_width'    => '150',
    'img_height'    => '30',
    'font_path'    => './instance/font/Elephant.ttf',
    'expiration'    => 7200,
    'word_length'    => 4,
    'font_size'    => 16,
    'img_id'    => '',
    'pool'        => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
    'colors'    => array(
        'background'    => array(255,255,255),
        'border'    => array(153,102,102),
        'text'        => array(204,153,153),
        'grid'        => array(255,182,182)
    )
);

// 将变量从数组中取出
foreach ($defaults as $key => $val){
        $$key = $val;
}

// 生成随机码
$word = '';
$pool_length = strlen($pool);
$rand_max = $pool_length - 1;
for ($i = 0; $i < $word_length; $i++){
    $word .= $pool[mt_rand(0, $rand_max)];
}


// 验证码长度
$length    = strlen($word);

//角度
$angle    = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
$x_axis    = mt_rand(6, (360/$length)-16);
$y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);

// 创建图片句柄
$im = p_w_picpathcreatetruecolor($img_width, $img_height);

// 创建颜色
foreach ($colors as $key => $values)
{
    $colors[$key] = p_w_picpathcolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
}


// 画了个和图片等大的矩形,就是背景
ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);


// -----------------干扰线--------------------------
$theta        = 1;
$thetac        = 7;
$radius        = 16;
$circles    = 20;
$points        = 32;

for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
{
    $theta += $thetac;
    $rad = $radius * ($i / $points);
    $x = ($rad * cos($theta)) + $x_axis;
    $y = ($rad * sin($theta)) + $y_axis;
    $theta += $thetac;
    $rad1 = $radius * (($i + 1) / $points);
    $x1 = ($rad1 * cos($theta)) + $x_axis;
    $y1 = ($rad1 * sin($theta)) + $y_axis;
    p_w_picpathline($im, $x, $y, $x1, $y1, $colors['grid']);
    $theta -= $thetac;
}
// --------------------------------------------------------------

// 无字体
// ($font_size > 5) && $font_size = 5;
// $x = mt_rand(0, $img_width / ($length / 3));
// $y = 0;
// for ($i = 0; $i < $length; $i++){
//     $y = mt_rand(0 , $img_height / 2);
//     p_w_picpathstring($im, $font_size, $x, $y, $word[$i], $colors['text']);
//     $x += ($font_size * 2);
// }



// 有字体
// x 第一个字符随机在图片的前面一小段,后面依次加一个字符大小的宽度
// y 图片的1/2到距离底部3px之间随机
($font_size > 30) && $font_size = 30;
$x = mt_rand(0, $img_width / ($length / 1.5));
$y = $font_size + 2;
for ($i = 0; $i < $length; $i++){
    $y = mt_rand($img_height / 2, $img_height - 3);
    p_w_picpathttftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
    $x += $font_size;
}


//画矩形框
p_w_picpathrectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);



// 删除以前保存的图片
$now = microtime(TRUE);

$current_dir = opendir($img_path);
while ($filename = readdir($current_dir)){
    if (in_array(substr($filename, -4), array('.jpg', '.png'))&& (str_replace(array('.jpg', '.png'), '', $filename) + 0.1) < $now){
        @unlink($img_path.$filename);
    }
}
@closedir($current_dir);









// 拼接文件路径并保存
$img_url = rtrim($img_url, '/').'/';
$img_filename = $now.'.jpg';
p_w_picpathjpeg($im, $img_path.$img_filename);

$img = '<img  src="'.$img_url.$img_filename.'"  alt=" " />';

// ImageDestroy($im);

// return array('word' => $word, 'time' => $now, 'p_w_picpath' => $img, 'filename' => $img_filename);

// echo $img;

//通知浏览器是png格式
header('Content-type:p_w_picpath/jpg');
//以png格式输出
p_w_picpathjpeg($im);
//销毁内存中的图片资源
p_w_picpathdestroy($im);


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI