国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 PHP: Use the gd library to generate images and implement random verification codes

PHP: Use the gd library to generate images and implement random verification codes

Oct 10, 2016 am 10:23 AM

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:

PHP: Use the gd library to generate images and implement random verification codesPHP: Use the gd library to generate images and implement random verification codesPHP: Use the gd library to generate images and implement random verification codes

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
// 創(chuàng)建畫布
$width = 120;   // 規(guī)定畫布的寬高
$height = 45;
$image = imagecreatetruecolor($width, $height);  // 創(chuàng)建一幅真彩色圖像
// 添加一些即將用到的顏色
$white = imagecolorallocate($image, 0xf2, 0xec, 0xe0);
$orange = imagecolorallocate($image, 0xff, 0xa5, 0x4c);
// 對畫布背景填充顏色
imagefill($image, 0, 0, $white);

//mt_rand  獲取隨機數(shù) 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)];  //如:隨機數(shù)為30  則:$str[30]
        }
        return $rand;
    }
$verifyCode=str_rand();
// 畫一串字符串在畫布上
imagestring($image, 10, 10, 10, "$verifyCode", $orange);
// 通知瀏覽器輸出的是圖像(png類型)
header(&#39;Content-Type: image/png&#39;);
// 輸出到瀏覽器
imagepng($image);
// 釋放圖像資源


Statement of this Website
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276