首页 > php教程 > PHP源码 > 正文

php制作验证码图片

PHP中文网
发布: 2016-05-23 08:39:28
原创
1378人浏览过

1. [代码]制作随机字符串   

<?php
print_r(join("",array_merge(range("a","z"),range("A","Z"))));
echo '<br />';

$chars = join("",array_merge(range("a","z"),range("A","Z"),range("0","9")));
$chars = str_shuffle($chars);
echo $chars;

foreach (range(0, 12) as $number) {
    echo $number,'<br />';
}
?>
登录后复制

           

2. [代码]利用GD库制作验证码   

<?php
require_once("string_func.php");
//通过GD库做验证码
//创建画布
/*
 * @param $sess_name SESSION名称
 * @param $type 验证码样式. 1:纯数字验证码; 2:纯字母验证码; 3:数字和字母混合验证码;
 * @param $length 验证码字符数量。
 * @param $pixel 干扰点数量
 * @param $line 干扰线数量
 * */
function image_func($sess_name = "verify",$type = 1,$length = 4,$pixel = 50,$line=3)
{
    session_start();
    $width = 80;
    $height = 30;

    $image = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);
//填充画布
    imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white);

    $chars = buildRandomString($type, $length);


    $_SESSION[$sess_name] = $chars;
    $fontfiles = array("msyh.ttc", "msyhbd.ttc", "simhei.ttf", "simsun.ttc", "Deng.ttf", "Dengb.ttf", "Dengl.ttf");
    for ($i = 0; $i < $length; $i++) {
        $size = mt_rand(14, 18);
        $angle = mt_rand(-15, 15);
        $x = 5 + $i * $size;
        $y = mt_rand(20, 26);
        $fontfile = "fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];
        $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 120), mt_rand(90, 180));
        $text = substr($chars, $i, 1);
        imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
    }

    if ($pixel) {
        for ($i = 0; $i < $pixel; $i++) {
            $pointer_color = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(0, 80), mt_rand(0, 30), $pointer_color);
        }
    }

    if ($line) {
        for ($i = 0; $i < $line; $i++) {
            $line_color = imagecolorallocate($image, mt_rand(100, 220), mt_rand(100, 220), mt_rand(100, 220));
            imageline($image, mt_rand(0, 80), mt_rand(0, 30), mt_rand(0, 80), mt_rand(0, 30), $line_color);
        }
    }

    header("content-type:image/gif");
    imagegif($image);
    imagedestroy($image);
}


return image_func('a',1,4,100,3);



?>
登录后复制

                

3. [代码]效果测试   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
@@##@@
</body>
</html>
登录后复制

                   

立即学习PHP免费学习笔记(深入)”;

                   

php制作验证码图片
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号