资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PHP碎码——ci的验证码

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

创新互联-专业网站定制、快速模板网站建设、高性价比沂南网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式沂南网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖沂南地区。费用合理售后完善,十年实体公司更值得信赖。

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

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

 '',
    '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 = '';

// 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);

新闻标题:PHP碎码——ci的验证码
标题来源:http://cdkjz.cn/article/jihdje.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220