PHP: 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 to be obtained, 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 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:
<?php // 创建画布 $width = 120; // 规定画布的宽高 $height = 45; $image = imagecreatetruecolor($width, $height); // 创建一幅真彩色图像 // 添加一些即将用到的颜色 $white = imagecolorallocate($image, 0xf2, 0xec, 0xe0); $orange = imagecolorallocate($image, 0xff, 0xa5, 0x4c); // 对画布背景填充颜色 imagefill($image, 0, 0, $white); //mt_rand 获取随机数 mt_rand(min, max); function str_rand(){ $str="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ"; $rand=""; for($i=0; $i<5; $i++){//获取5个随机的字符串 $rand .= $str[mt_rand(0, strlen($str)-1)]; //如:随机数为30 则:$str[30] } return $rand; } $verifyCode=str_rand(); // 画一串字符串在画布上 imagestring($image, 10, 10, 10, "$verifyCode", $orange); // 通知浏览器输出的是图像(png类型) header('Content-Type: image/png'); // 输出到浏览器 imagepng($image); // 释放图像资源

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.