关键词搜索

源码搜索 ×
×

PHP生成图像验证码

发布2020-06-13浏览351次

详情内容

code.php

<?php
function random($len) {
    $srcstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    mt_srand();
    $strs = "";
    for($i = 0; $i < $len; $i++) {
        $strs .= $srcstr[mt_rand(0, 35)];
    }
    return $strs;
}
$str = random(4); // 随机生成的字符串
$width = 50; // 验证码图片的宽度
$height = 25; // 验证码图片的高度

@header("Content-Type:image/png");

// echo $str;

$im = imagecreate($width, $height);

// 背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

// 模糊点颜色
$pix = imagecolorallocate($im, 187, 230, 247);

// 字体色
$font = imagecolorallocate($im, 41, 163, 238);

// 绘制模糊作用的点
mt_srand();
for($i = 0; $i < 1000; $i++) {
    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}

imagestring($im, 5, 7, 5, $str, $font);
imagerectangle($im, 0, 0, $width-1, $height-1, $font);
imagepng($im);
imagedestroy($im);
?>

    a.php

    <?php
    echo "<img src=code.php>"; // 生成图片
    ?>
    
    • 1
    • 2
    • 3

    将code.php和a.php赋值到phpEnv的根目录下,运行a.php:
    在这里插入图片描述

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载