search
Homephp教程php手册PHP study notes: Use the gd library to generate images and implement random verification codes

Note: I have commented some basic codes. The number of verification code digits and the required strings implemented here can be set again. With my annotations, it should be easy for everyone to understand.

Basic idea:

1. Use mt_rand() to randomly generate numbers to determine the string you need to obtain, and splice the strings (I think the generated verification code is a bit too crowded, you can splice a space bar in the middle of the string) to implement a random verification code ;

Note: It is recommended that you use mt_rand() instead of rand(), the former is more efficient

2. Use the gd library to generate pictures and write random strings to the picture output.

Effect:

Every time it is refreshed, a random verification is generated. Later I may add how to implement the random code and click on the picture to update again

Code:

<span style="font-size: 18px;"><span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建画布</span>
<span style="color: #800080;">$width</span> = 120;   <span style="color: #008000;">//</span><span style="color: #008000;"> 规定画布的宽高</span>
<span style="color: #800080;">$height</span> = 45<span style="color: #000000;">;
</span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 创建一幅真彩色图像
// 添加一些即将用到的颜色</span>
<span style="color: #800080;">$white</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xf2, 0xec, 0xe0<span style="color: #000000;">);
</span><span style="color: #800080;">$orange</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xa5, 0x4c<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对画布背景填充颜色</span>
imagefill(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$white</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">mt_rand  获取随机数 mt_rand(min, max);</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;">  str_rand(){
        </span><span style="color: #800080;">$str</span>="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ"<span style="color: #000000;">;
        </span><span style="color: #800080;">$rand</span>=""<span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>$i</span>++){<span style="color: #008000;">//</span><span style="color: #008000;">获取5个随机的字符串</span>
            <span style="color: #800080;">$rand</span> .= <span style="color: #800080;">$str</span>[<span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$str</span>)-1)];  <span style="color: #008000;">//</span><span style="color: #008000;">如:随机数为30  则:$str[30]</span>
<span style="color: #000000;">        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$rand</span><span style="color: #000000;">;
    }
</span><span style="color: #800080;">$verifyCode</span>=<span style="color: #000000;">str_rand();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一串字符串在画布上</span>
imagestring(<span style="color: #800080;">$image</span>, 10, 10, 10, "<span style="color: #800080;">$verifyCode</span>", <span style="color: #800080;">$orange</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通知浏览器输出的是图像(png类型)</span>
<span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出到浏览器</span>
imagepng(<span style="color: #800080;">$image</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放图像资源</span>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)