【实例】使用GD库生成图片验证码

一、加载 GD 库

GD 库是一个开放的、动态创建图像的、源代码公开的函数库,可以从官网下载。目前,GD 库支持 GIF、PNG、JPEG、WBMP 和 XBM 等多种图像格式,用于对图像的处理。
GD 库在 PHP5 是默认安装的,但要激活 GD 库,必需设置 php.ini 文件。即将该文件中的“;extension=php_gd2.dll”选项前的分号“;”删除。保存修改后的文件并重启 Apache 服务器即可。
在成功加载 GD2 函数库后,可以通过 phpinfo() 函数来获取 GD2 函数库的安装信息,验证 GD 库是否安装成功。在浏览器中输入“域名/phpinfo.php”并按 Enter 键,在打开的页面中如果检索到下图中的 GD 库的安装信息,说明 GD 库安装成功。
在这里插入图片描述

如果使用的是 PHP 集成开发环境,默认 GD2 函数库已经被加载。

二、开发步骤

1、使用 imagecreatetruecolor()函数创建一个图片(即画布)。
2、填充图片背景,使用 imagettftext()函数向图片添加随机验证码。
3、设置干扰,使用 imagesetpixel()函数给图片添加干扰点。
4、设置干扰,使用 imageline()函数给图片添加干扰线。
5、输出图片,然后释放资源。
6、最后在展示页面输出图片验证码。

三、代码

1、验证码生成文件:code.php

<?php
//启动session
session_start();
$code = "";         //验证码字符串
//验证码字符取值范围[a-z0-9]
$str = "qwertyuiopasdfghjklzxcvbnm1234567890";
$w = 160;           //图片宽度
$h = 40;            //图片高度
$num = 4;           //验证码字符数
$dotNum = 300;      //干扰点个数
$lineNum = rand(3, 5);  //干扰线条数
$font = "simsunb.ttf";  //设置字体文件
$image = imagecreatetruecolor($w, $h);  //创建一张指定宽高的图片
//设置背景图片颜色为白色
$imageColor = imagecolorallocate($image, 255, 255, 255);   
//填充图片背景
//随机验证码,包含字母和数字
imagefill($image, 0, 0, $imageColor);  
for ($i = 0; $i < $num; $i++) {
    $fontColor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));  // 生成随机字体颜色
    //随机取字符集中的值
    $content = substr($str, rand(0, strlen($str)), 1);      
    $code .= $content;
    $fontSize = rand(15, 25);   // 字体大小
    // 指定生成位置X轴偏移量
    $x = $i * $w / $num + rand(5, 10);          
    $y = rand(20, 30);  // 指定生成位置Y轴偏移量
    imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $font, $content);
}
$_SESSION["code"] = $code;  //保存验证码字符串到session中
//生成干扰点
for ($i = 0; $i < $dotNum; $i++) {
    $dotColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $w), rand(0, $h), $dotColor);
}

//生成干扰线
for ($i = 0; $i < $lineNum; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));

    imageline($image, rand(0, $w), rand(0, $h), rand(0, $w), rand(0, $h), $lineColor);
}
header("content-type:image/png");
imagepng($image);
imagedestroy($image);

2、图片验证码应用文件:front.php

<?php
    if(isset($_REQUEST["code"])){
        session_start();
        if(strtolower($_POST["code"])==$_SESSION["code"]){
            echo "<script>alert('正确!')</script>";
        }else{
            echo "<script>alert('错误!')</script>";
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>验证码</title>
    <style>
        #code{
            border: 1px solid #ccc;
            vertical-align: bottom;
        }
        #refresh{
            text-decoration: none;
            font-size: .875em;
        }
    </style>
</head>
<body>
    <form action="" method="post">
        <p>
            验证码:
            <img src="code.php?r=<?php echo rand()?>" alt="" id="code">
            <a href="javascript:;" id="refresh">看不清?</a>
        </p>
        <p>
            输入验证码:
            <input type="text" name="code">
        </p>
        <input type="submit" value="提交">
        <script>
        	// 刷新验证码
            document.getElementById("code").onclick = document.getElementById("refresh").onclick = refresh;
            function refresh() {
                document.getElementById('code').src='code.php?r='+Math.random()
            }
        </script>
    </form>
</body>
</html>
四、效果展示

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值