Detailed explanation of the function of generating image verification code in PHP

墨辰丷
Release: 2023-03-28 08:36:02
Original
1755 people have browsed it

This article mainly introduces the function of PHP to generate image verification codes, and briefly introduces the operation skills of PHP to generate verification code images in the form of examples. It has certain reference value. Friends in need can refer to the following

for details As follows:

It simply uses a random function to generate images, without introducing the entire verification process.

The code is as follows:

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: lee
 * To change this template use File | Settings | File Templates.
 */
header("content-type:image/png");
$validateLength=4;
$strToDraw="";
$chars=[
  "0","1","2","3","4",
  "5","6","7","8","9",
  "a","b","c","d","e","f","g",
  "h","i","j","k","l","m","n",
  "o","p","q","r","s","t",
  "u","v","w","x","y","z",
  "A","B","C","D","E","F","G",
  "H","I","J","K","L","M","N",
  "O","P","Q","R","S","T",
  "U","V","W","X","Y","Z"
];
$imgW=80;
$imgH=25;
$imgRes=imagecreate($imgW,$imgH);
$imgColor=imagecolorallocate($imgRes,255,255,100);
$color=imagecolorallocate($imgRes,0,0,0);
for($i=0;$i<$validateLength;$i++){
  $rand=rand(1,58);
  $strToDraw=$strToDraw." ".$chars[$rand];
}
imagestring($imgRes,5,0,5,$strToDraw,$color);
for($i=0;$i<100;$i++){
  imagesetpixel($imgRes,rand(0,$imgW),rand(0,$imgH),$color);
}
imagepng($imgRes);
imagedestroy($imgRes);
Copy after login

The running effect is as follows:

The above is this article The entire content, I hope it will be helpful to everyone's study.


Related recommendations:

php implements websiteVerification codefunction

php encapsulatedVerification codeClass detailed explanation

php implements encapsulatedVerification codeClass

The above is the detailed content of Detailed explanation of the function of generating image verification code in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!