GD library generates image verification code
We are no strangers to verification codes. They can be seen everywhere, such as: login and registration, forum flooding, ticket brushing, password cracking, etc. Their main function is to block machine requests and protect business from being interfered by machine submission requests.
Let’s write a verification code demo, using the most common alphanumeric verification code, plus interference points and interference lines. It is generated by using the GD library. If you have not installed it, please install it on Google yourself. , and how to determine whether it is installed and enabled, please search the GD library directly on the phpinfo page
The effect is as shown below:
##Front page
<?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> <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="/static/imghw/default1.png" data-src="code.php?r=<?php echo rand()? alt="GD library generates image verification code" >" class="lazy" 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>
Backend page
<?php //启动session session_start(); $code = ""; //验证码字符串 $str = "qwertyuiopasdfghjklzxcvbnm1234567890"; //验证码字符取值范围[a-z0-9] $w = 160; //图片宽度 $h = 40; //图片高度 $num = 4; //验证码字符数 $dotNum = 300; //干扰点个数 $lineNum = rand(3, 5); //干扰线条数 $font = "./api/DejaVuSansMono.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 = $i * $w / $num + rand(5, 10); //指定生成位置X轴偏移量 $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);
The above is the detailed content of GD library generates image verification code. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. What is the GD library? The GD library is a set of library functions for creating and processing various image formats. It is one of the most commonly used image processing libraries in PHP. 2. Install the GD library Install the GD library under CentOS/RedHat 1. Install PHP’s GD extension library yuminstallphp-gd 2. Restart the web server servicehttpdrestart 3. Check the GD library version supported by PHP php-i | grep-igd in Ubunt

The solution to the problem that PHP cannot open the gd library: 1. Find and open the php.ini configuration file; 2. Remove the comment symbol ";" in front of "extension_dir"; 3. Change its value to the absolute path of the ext folder. .

How to implement image rotation using PHP and GD libraries Image rotation is a common image processing requirement. By rotating images, you can achieve some special effects or meet user needs. In PHP, you can use the GD library to implement the image rotation function. This article will introduce how to use PHP and the GD library to implement image rotation, with code examples. First, make sure your PHP environment has the GD library extension installed. Enter php-m on the command line to check if there is a gd module. If not, you need to install it first. Here is a simple

Title: Steps to create image thumbnails using PHP and GD library Introduction: In web development, images often need to be thumbnailed to adapt to different page layouts. This article will introduce the steps on how to use PHP and GD library to create image thumbnails, and attach relevant code examples. 1. Install and configure the GD library. The GD library is a library for image processing. You can use some simple functions to process images. Before we begin, we need to ensure that the GD library is properly installed and configured. Check whether the GD library has been installed: execute in PHP script

Introduction to the method of realizing rounded corner images using PHP and GD libraries. In web design, sometimes it is necessary to use rounded corner images to beautify the appearance of the page. This article will introduce how to use PHP and GD library to implement rounded images. The GD library is one of the PHP extension libraries and provides a series of functions for processing images. By using the GD library, we can crop, resize, add filters, etc. to images. To achieve rounded images, we need to use some functions in the GD library for image processing. The following are the specific steps to achieve rounded corner images.

Overview of how PHP and GD libraries implement image cropping: Image cropping is one of the common requirements in web development. It can be used to adjust the size of images and crop unnecessary parts to adapt to different page layouts and display needs. In PHP development, we can use the GD library to realize the image cropping function. The GD library is a powerful graphics library that provides a series of functions to process and manipulate images. Code example: Below we will introduce in detail how to use PHP and GD library to implement image cropping. First, make sure your PHP environment has

PHP and GD library tutorial: How to add blur effects to images Overview: In web development, images often need to be processed, and one of them is to add blur effects. PHP provides a powerful GD library that allows us to easily blur images. This tutorial will show you how to add a blur effect to an image using PHP and the GD library, with code examples. Step 1: Set up the GD library. To use the GD library, we need to ensure that the GD library has been enabled in PHP. You can check whether the GD library has been enabled through the following code: if(

Use PHP and GD libraries to generate random background images. Random background images play an important role in web design and can increase the beauty and appeal of the page. This article will introduce how to use PHP and the GD library to generate random background images. The GD library is a PHP extension module for image processing that can create, edit and manipulate images in PHP. By combining the powerful functions of the GD library, we can easily generate random background images in various styles. First, we need to install the GD library on the server. You can check it with the following command
